You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Synchronization types like Semaphore[Slim], Mutex etc. should be used with try/finally after acquiring the lock, with the release operation happening in the finally block. If an exception occurs in the guarded section, the release operation is never called, leaving the object locked for longer than necessary, potentially even forever (deadlock). The finally block guarantees that the lock is always released.
Noncompliant code snippet
privatereadonlySemaphoreSlim_semaphore;// ------await_semaphore.WaitAsync();// The synchronized code_semaphore.Release();
Compliant code snippet
privatereadonlySemaphoreSlim_semaphore;// ------await_semaphore.WaitAsync();try{// The synchronized code}finally{_semaphore.Release();}