Reputation: 1347
Is there a way to benefit from the advantages of non-blocking thread synchronization, such as that described here, but while using the System.Threading.Task
objects in the current .Net framework versions?
What i'm looking for essentially is changing this code
new Task(() =>
{
lock (_lock)
{
...
}
});
into something that won't block the threadpool thread that will be used by the Task
in performing the operation protected by the lock.
Upvotes: 0
Views: 821
Reputation: 244767
If you want to do this using .Net 4.0 TPL, you can use a custom TaskScheduler
, for example LimitedConcurrencyLevelTaskScheduler
from ParallelExtensionsExtras.
If you can use C# 5.0, you could use await
together with SemaphoreSlim.WaitAsync()
or AsyncLock
from Nito AsyncEx.
Upvotes: 2