Skip to content

Async Primitives Package

kobi2294 edited this page Apr 22, 2019 · 1 revision

Async Primitives Package

Shared resource protection is an integral part of asynchronous programming. It is based on the concept of a Mutex - an object that serves as lock to prevent mutual access to the same resource. TPL, on the other hand, is an asynchronous model that avoids blocking threads. Furthermore, an async operation in TPL may start in one thread and continue on another, so the concept of a thread owning mutual exclusive access to a resource - will not work in TPL.

In order to overcome that, we need a different behavior. Instead of a resource owned by a thread, we want it to be owned by a task. That way, once the lock is aquired, it remains property of the task no matter at which thread it continues. Also, while a task waits for a lock to be freed, it releases the thread it runs on, and no thread ever becomes locked.

In order to do that, we need a different set of synchronization primitives. In traditional asynchronous development, we use Mutex, Semaphore and Reader-Writer locks. In TPL we will use parallel classes that are designed to work according to the TPL paradigm.

Classes

  • AsyncMutex - An asynchronous mutex, can be aquired once at a time.
  • AsyncSemaphore - Similar to AsyncLock but allows a specified level of parallelism
  • AsyncReaderWriterLock - Similar to AsyncMutex but handles two sets of locks: One for readers, allowing concurrent access to the resource, and one for writers, forcing exclusive access to the resource.
Clone this wiki locally