Semaphore is used to control the number of concurrent threads to access a particular section of the code / resources. With the ability to control the number of concurrent threads, It can do more than just acquiring exclusive lock by limiting to 1 thread. I will demonstrate on how to use Semaphore to control the concurrent threads with an example. The example below is by no means the best practice, it is just to show how Semaphore works.
Imagine that you need to process more than 1 file at the same time with FileSystemWatcher. By default, the event only process 1 file at a time. So to process multiple file at the same time and control the threads, we will use Task.Run to simulate fire-and-forget and Semaphore to control the threads.
As usual, you need an instance of Semaphore. The following constructor accept 2 parameters. The first parameter define the number of free / available requests can be accepted. The second parameter define the maximum number of concurrent threads. In this case, we have 3 available requests and can accept only up to 3 concurrent threads.
[C#]
Semaphore semaphore = new Semaphore(3, 3);
[VB]
Dim semaphore As Semaphore = New Semaphore(3,
3)
In your FileSystemWatcher's created event, call WaitOne() to occupy 1 available request. If the request is not available, it will wait until a new request is available.
[C#]
semaphore.WaitOne();
[VB]
semaphore.WaitOne()
We will use Task.Run to execute the method in another thread. In that method, call Release() to release the request. Allowing another thread to acquire the available request.
[C#]
semaphore.Release();
[VB]
semaphore.Release()
You can get the sample code from the following link. https://onedrive.live.com/redir?resid=E6612168B803803D!346&authkey=!AAsJ_f-95sQxOpk&ithint=file%2czip
If you play around with the sample code, you will notice that the thread id is different from when it call WaitOne() and call Release(). This is due to the thread that execute Task.Run spawn a new thread and since it did not call await or .Wait() for the Task.Run, it end up leaving the Task.Run without waiting for its completion and exit the method. To re-explain this in steps,
- Thread A call WaitOne() and execute Task.Run.
- Task.Run spawn a new thread B to further execute the method specified in Task.Run.
- Thread A leaves without waiting thread B completes its execution and exit the method. (fire-and-forget)
- Thread B executing the method and call Release().
No comments:
Post a Comment