Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip SemaphoreSlim instead of wait

I have a part of code in an Async/Await function that I only want one thread to execute at a time.

This is relatively simple by creating a new SemaphoreSlim(1) and using WaitAsync/Release. The effect is that the first thread executes while the others wait and then execute one by one.

What I am trying to achieve is actually slightly different. I would like the other threads not to wait, but to return out of the function (i.e. I don't want to block the other threads). So if there was a property "NumberOfThreadsCurrentlyExecuting" I would effectively have an If Semaphore.NumberOfThreadsCurrentlyExecuting > 0 Then Return.

But such a property doesn't exist. Does anyone have any idea for a way around this problem?

Thanks Charles

like image 316
Charles Avatar asked Aug 13 '14 22:08

Charles


1 Answers

How about using the SemaphoreSlim.Wait/Async with a zero-timeout? If it can't enter the semaphore (because it's already been entered), it will return false.

Note that Monitor (and thus lock) is completely unsuited to async

(hence the fact that you can't await in a lock) because

  1. your task may continue on another thread after you've entered the lock (thus you will try to release the lock from another thread)
  2. after you've awaited, another continuation may use your thread (while it is still holding the lock), so if it attempts to acquire the lock it will succeed
like image 122
Mark Sowul Avatar answered Oct 31 '22 13:10

Mark Sowul