Introduction

edit

Meziantou.Framework.Threading provides the synchronization primitives that .NET does not have out of the box for asynchronous code, a few thread-safe collections, and helpers that make awaiting tasks and scheduling work simpler.

What you can do with it

Principles

  • Asynchronous waits. The asynchronous locks and the event never block a thread while waiting: the waiting method is suspended and resumed when its turn comes. AsyncLock and AsyncAutoResetEvent serve waiters in the order they started waiting.
  • Cancellable. Every asynchronous wait accepts a CancellationToken, so a caller can give up waiting.
  • Released by using. The locks return a lease that releases them when disposed. Disposing a lease a second time does nothing, except for the SemaphoreSlim methods whose name says Unsafe.
  • Continuations run asynchronously by default. Releasing a lock or setting the event does not run the code of the next waiter on the releasing thread, unless you opt in.

Namespaces

NamespaceContains
Meziantou.Framework.ThreadingAsyncLock, AsyncReaderWriterLock, KeyedLock<TKey>, KeyedAsyncLock<TKey>, AsyncAutoResetEvent, ResettableCancellationTokenSource, MonoThreadedTaskScheduler, MixedConsumerProducer, and the SemaphoreSlim and SynchronizationContext extensions
Meziantou.Framework.Threading.TasksTaskExtensions: awaiting tuples of tasks, WhenAll for ValueTask, Forget
Meziantou.Framework.Collections.ConcurrentConcurrentHashSet<T>, SynchronizedList<T>, ConcurrentObservableCollection<T>

Next steps