1

Using SysV semaphores in a UNIX OS it is possible to implement an algorithm in which a process waits until the value of a semaphore reaches the value 0 (see man semop for details). Now, everybody seems to say that it is better to use other synchronization APIs, and so my question is: how can I obtain the same behavior using pthreads, POSIX semaphores or other similar APIs? If it is not possible, should I be worried about using SysV semaphores?

EDIT: I'm referring to a multithreaded scenario, threads are created using pthreads.

Raffo
  • 1,642
  • 6
  • 24
  • 41

1 Answers1

0

Sysv semaphores are great for multi-process synchronization. There is no drop in substitute.

If you have a single process and use threads, then sysv semaphores have a large overhead and consume global system resources when you don't need to. I would simply use POSIX semaphores.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • Edited the question. I'm in a multithreaded scenario. I can use posix semaphores, but I can't see an obvious way to obtain a synchronization on the value 0 on the semaphore as described in the question. – Raffo Jul 28 '11 at 15:11
  • @Rafflo: are you trying to emulate a barrier as opposed to a semaphore? That is all resources waiting are ready to run when the barrier hits 0. – Yann Ramin Jul 28 '11 at 15:16
  • Yes, probably the same thing can be achieved using a barrier. However I'm currently on OSX where there's no memory barrier implementation in pthread. Another thing to notice is that I'm not aware of the value the semaphore is going to reach... I just want a thread to stop till it is not 0. – Raffo Jul 28 '11 at 15:20