views:

43

answers:

1

We have three threads or more, can we use only two semaphores (binary or counting semaphores)? An idea that popped into my mind is playing with the number of sem_post-s and sem_wait-s. Any ideas, strategies are welcomed.

+2  A: 

A semaphore is used to protect a shared resource. You need as many semaphores as shared resources, this is not linked to how many threads access these resources.

mouviciel
if you have concurrency between threads and the race condition occurs, one semaphore is not enough. i.e. there are three threads that run several iterations. If I want those three threads to execute in a particular order, say iteration 0 - threadA, threadB, threadC; iteration 1 - threadA, .... multiple semaphores need to be implemented. So far I've found out that there can be two semaphores doing this. however, could it be done with only one?
bsuv
Why use threads if they are executed sequentially? answer to this question may reveal some hidden resources that you want to protect.
mouviciel