YohanRoth
YohanRoth

Reputation: 3263

Why and when do we need to use semaphores with value > 1?

I am confused why semaphores work when we have value > 1 and how they protect data from inconsistency. I know that semaphore with value 1 is the same as mutex. We use mutex to protect Critical Sections in out code, especially when we need to protect data from inconsistency (meaning one thread can work on a data in a given time). But semaphores with value > 1 give access to the same Critical Section for several threads... And here I do not understand how it works. How can we really protect our data from inconsistency allowing only limited amount of threads operate on it in a given time. Those threads can still mess smth without mutual exclusion. So, what is the point of using semaphore with value > 1? When do we want to use it over mutexes and why.

Thank you

Upvotes: 2

Views: 1169

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 134055

Imagine your application has multiple threads that are communicating with an external server. That server allows your application to have up to five outstanding requests at any time. If you make six requests really quickly--that is, the sixth request occurs before the server responds to any of the first five--the server will block your application for 15 minutes.

You can control that with a semaphore. You set the semaphore's initial value to 5. When a thread wants to make a request of the server, it waits on the semaphore. When the wait returns, the semaphore count is decreased by one. When the semaphore count gets to 0, a thread that waits will be blocked until some other thread releases it.

So you have:

sem = new Semaphore(5); // create semaphore with initial value of 5

When a thread wants to make a request:

sem.Wait();  // waits on semaphore. If there are 5 requests already, this will block
MakeRequest();
GetResponose();
sem.Release();  // release the semaphore, which will increment the count

Now, if six threads try to make requests in a very short time, the first five will be allowed, and the sixth will have to wait until one of those threads calls sem.Release().

The real question is why you'd create a semaphore with a maximum value less than two. If you want to do mutual exclusion, then you should use a mutex. A semaphore with a value of one is similar to a mutex, but not at all the same thing. In particular, a mutex guarantees mutual exclusion because the thread that acquires the lock must be the one to release it. If a thread tries to release a mutex that it didn't acquire, it will fail. But with a semaphore, any thread can call Release, even if it hadn't previously called Wait.

Upvotes: 4

bhdeeps
bhdeeps

Reputation: 101

When we have a resource that can be shared by multiple threads at a time, you use a semaphore. Example: Consider the readers-writers problem. Here the resource is a buffer with more than one location that can be written and read, we use semaphores to ensure we have something to write before we read and we have space to write before we write.

Upvotes: 0

Related Questions