I have some doubts as to whether the “library” method below will work, or, ultimately, a dead end.
I have an “old” object that needs to be protected, and “ locking ” seems to be the best tool, given that there are some reentrant calls to this resource (SomeSharedResource) throughout the “class”.
EDIT:
1.) Will the call “Calculate” actually block or the worst case block?
var finalResult = await Compute(3).ConfigureAwait(false);
2.) How about the one who did this?
var finalResult = Compute(3).Result;
3.) 2 Concurrent thread calls:
var finalResult = await Compute(3).ConfigureAwait(false);
This method:
private readonly object lockObject = new object();
private async Task<double> Compute(int input)
{
double result;
lock (lockObject) {
result = SomeSharedResource(input);
}
return await ComputeAsync(result).ConfigureAwait(false);
}
source
share