Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of the initial value of a semaphore?

A semaphore is a mechanism for avoiding race conditions. But what's the significance of the initial value of a semaphore?

Say that a semaphore has an initial value of 5, is it that 5 processes can access some shared resource simultaneously?

like image 833
freenight Avatar asked Jan 23 '23 07:01

freenight


2 Answers

My knowledge of semaphores is rusty, but if you create a semaphore with an initial count of 5, it means that 5 threads (not processes) can access the semaphore simultaneously. Have a look at these links for some more details:

  • http://msdn.microsoft.com/en-us/library/ms685129(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/ms686946(VS.85).aspx
  • http://msdn.microsoft.com/en-us/library/z6zx288a.aspx
like image 92
Jason Evans Avatar answered Mar 15 '23 10:03

Jason Evans


Semaphores are a way of coordinating multiple threads of control, not just for mutual exclusion. For example, a classical fixed-size producer-consumer queue may use a semaphore initialized to a non-zero value for producers so that they block when there are too many elements in the buffer.

like image 43
Barry Kelly Avatar answered Mar 15 '23 08:03

Barry Kelly