All Projects → neosmart → AsyncLock

neosmart / AsyncLock

Licence: MIT license
An async/await-friendly lock for .NET, complete with asynchronous waits, safe reëntrance, and more.

Programming Languages

C#
18002 projects
shell
77523 projects

Projects that are alternatives of or similar to AsyncLock

janus-gateway-live
RTMP edge speed with janus-gateway
Stars: ✭ 38 (-64.15%)
Mutual labels:  synchronization
ParseCareKit
Securely synchronize any CareKit 2.1+ based app to a Parse Server Cloud. Compatible with parse-hipaa.
Stars: ✭ 28 (-73.58%)
Mutual labels:  synchronization
triple-buffer
Implementation of triple buffering in Rust
Stars: ✭ 66 (-37.74%)
Mutual labels:  synchronization
relaks
Asynchrounous React component
Stars: ✭ 49 (-53.77%)
Mutual labels:  await
watchdb
Keeping SQLite databases in sync
Stars: ✭ 72 (-32.08%)
Mutual labels:  synchronization
mine
Share application state across computers using Dropbox.
Stars: ✭ 14 (-86.79%)
Mutual labels:  synchronization
locize-cli
locize cli to import / export locales, add / edit / remove sync segments
Stars: ✭ 44 (-58.49%)
Mutual labels:  synchronization
synchronicity
Synchronicity lets you interoperate with asynchronous Python APIs.
Stars: ✭ 41 (-61.32%)
Mutual labels:  await
sleepover
💤 Sleep, snooze & step methods
Stars: ✭ 13 (-87.74%)
Mutual labels:  await
of
🍬 Promise wrapper with sugar 🍬
Stars: ✭ 13 (-87.74%)
Mutual labels:  await
checksync
A tool for detecting when related text blocks change
Stars: ✭ 14 (-86.79%)
Mutual labels:  synchronization
Promise.allSettled
ES Proposal spec-compliant shim for Promise.allSettled
Stars: ✭ 93 (-12.26%)
Mutual labels:  await
retrygroup
Package retrygroup provides synchronization, Context cancelation for groups of retry goroutines working on subtasks of a common task.
Stars: ✭ 18 (-83.02%)
Mutual labels:  synchronization
braid-spec
Working area for Braid extensions to HTTP
Stars: ✭ 179 (+68.87%)
Mutual labels:  synchronization
helo
A simple and small low-level asynchronous ORM using Python asyncio.
Stars: ✭ 18 (-83.02%)
Mutual labels:  await
flow
C++14, header-only library for multi-stream data synchronization.
Stars: ✭ 26 (-75.47%)
Mutual labels:  synchronization
ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (-81.13%)
Mutual labels:  await
in2publish core
in2publish Community Version
Stars: ✭ 38 (-64.15%)
Mutual labels:  synchronization
AudioAlign
Audio Synchronization and Analysis Tool
Stars: ✭ 80 (-24.53%)
Mutual labels:  synchronization
TsukiSuite
A toolsuite created to make Unity development easier
Stars: ✭ 23 (-78.3%)
Mutual labels:  nuget-package

AsyncLock: An async/await-friendly lock

NuGet

AsyncLock is an async/await-friendly lock implementation for .NET Standard, making writing code like the snippet below (mostly) possible:

lock (_lockObject)
{
    await DoSomething();
}

Unlike most other so-called "async locks" for C#, AsyncLock is actually designed to support the programming paradigm lock encourages, not just the technical elements. You can read more about the pitfalls with other so-called asynchronous locks and the difficulties of creating a reentrance-safe implementation here.

With AsyncLock, you don't have to worry about which thread is running what code in order to determine whether or not your locks will have any effect or if they'll be bypassed completely, you just write code the way you normally would and you'll find AsyncLock to correctly marshal access to protected code segments.

Using AsyncLock

There are only three functions to familiarize yourself with: the AsyncLock() constructor and the two locking variants Lock()/LockAsync() .

AsyncLock() creates a new asynchronous lock. A separate AsyncLock should be used for each "critical operation" you will be performing. (Or you can use a global lock just like some people still insist on using global mutexes and semaphores. We won't judge too harshly.)

Everywhere you would normally use lock (_lockObject) you will now use one of

  • using (_lock.Lock()) or
  • using (await _lock.LockAsync())

That's all there is to it!

Async-friendly locking by design

Much like theSemaphoreSlim class, AsyncLock offers two different "wait" options, a blocking Lock() call and the asynchronous LockAsync() call. The utmost scare should be taken to never call LockAsync() without an await before it, for obvious reasons.

Upon using LockAsync(), AsyncLock will attempt to obtain exclusive access to the lock. Should that not be possible in the current state, it will cede its execution slot and return to the caller, allowing the system to marshal resources efficiently as needed without blocking until the lock becomes available. Once the lock is available, the AsyncLock() call will resume, transferring execution to the protected section of the code.

AsyncLock usage example

private class AsyncLockTest
{
    var _lock = new AsyncLock();

    void Test()
    {
        // The code below will be run immediately (likely in a new thread)
        Task.Run(async () =>
             {
                 // A first call to LockAsync() will obtain the lock without blocking
                 using (await _lock.LockAsync())
                 {
                     // A second call to LockAsync() will be recognized as being
                     // reentrant and permitted to go through without blocking.
                     using (await _lock.LockAsync())
                     {
                         // We now exclusively hold the lock for 1 minute
                         await Task.Delay(TimeSpan.FromMinutes(1));
                     }
                 }
             }).Wait(TimeSpan.FromSeconds(30));

        // This call to obtain the lock is made synchronously from the main thread.
        // It will, however, block until the asynchronous code which obtained the lock
        // above finishes.
        using (_lock.Lock())
        {
            // Now we have obtained exclusive access.
            // <Safely perform non-thread-safe operation safely here>
        }
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].