Question 1:
Is the lock
really necessary in this situation?
var combinedResponse = new Response();
foreach (var server in servers)
{
Response r = await sqlExecutor.ExecQuery(query, server);
lock (combinedResponse) // is the lock necessary here?
{
combinedResponse.Merge(r); //knowing that we must not merge 2 results in parallel
}
}
I don't know which is true?
1) the codes after await
is executed in the same thread with the codes before await
(=> so the lock is not neccessary)
2) the codes after await
might be executed in a totally different thread than the codes before await
(=> so the lock is neccessary)
Question 2:
In my case the ExecQuery
is an IO-bound task, it won't spawn any additional thread. But if it wasn't the case (if ExecQuery
was a CPU-bound task which might spawn many additional threads) then would I need to lock
after await
?