C# Semaphore set the limit of thread that can enter into a critical section. We use it where have limited number of resources and we have to limit the number of threads that can use at a time.
The count on semaphore decremented each time when a thread enter and incremented when a thread exit. When the count is zero it means no slot is empty for new thread and new thread have to wait until other thread released. When all thread are released than it have maximum value specified at time on creation.
The count on semaphore decremented each time when a thread enter and incremented when a thread exit. When the count is zero it means no slot is empty for new thread and new thread have to wait until other thread released. When all thread are released than it have maximum value specified at time on creation.
Below is the syntax of C# semaphore initialization.
Another overload is
Example:-
Semaphore semaphoreObj = new Semaphore(initialCount: 0, maximumCount: 5);First parameter specify how many slot are available at the time of object instantiated. The second parameter specify the maximum number of slots available. If you want to reserve some slots fo calling thread you can put first number smaller than second one. To make available all slots you should have both parameter same.
WaitOne Method
Thread can enter into critical section using WaitOne(..) of semaphore object. If count (Int32) value is not zero than it allow calling thread to enter.
Below is the syntax of calling WaitOne method.
semaphoreObject.WaitOne();
bool isSignalled = semaphoreObject.WaitOne(TimeSpan.FromSeconds(4));Time interval specify, if there is no slot available than the thread will wait upto given time. If calling thread does not receive any signal within given time than it return false. If it receive signal than return true.
Release Method
To exit from critical section calling thread call release method of semaphore.
semaphoreObject.Release();
Another overload is
semaphoreObject.Release(3);
Multiple thread can exit at same time. In above case three thread simultaneously can exit and increment the counter by 3.
Example:-
public
class
SemaphoreSample
{
// Creates a semaphore object that can support maximum of 4 concurrent
// requests. Initial count is set to 3, so only 3 slots available
// for other threads and 1 slot is reserved for current thread.
static
Semaphore semaphoreObject =
new
Semaphore(3, 4);
private
static
void
DoWork()
{
Console.WriteLine(
"{0} is waiting in QUEUE..."
, Thread.CurrentThread.Name);
// Thread waits to get an available slot
semaphoreObject.WaitOne();
Console.WriteLine(
"{0} enters the Critical Section!"
, Thread.CurrentThread.Name);
// Simulate work by waiting for 1 second.
Thread.Sleep(1000);
Console.WriteLine(
"{0} is leaving the Critical Section"
, Thread.CurrentThread.Name);
// Release the slot.
semaphoreObject.Release();
}
public
static
void
Main()
{
// Create 10 threads, Set their name property
// and start DoWork on each thread.
for
(
int
i = 0; i < 10; i++)
{
Thread thread =
new
Thread(DoWork);
thread.Name =
"Thread_"
+ i;
thread.Start();
}
Console.ReadLine();
}
}
No comments:
Post a Comment