pthreads mutex vs semaphore

cppdev picture cppdev · Jan 14, 2010 · Viewed 78.5k times · Source

What is the difference between semaphores and mutex provided by pthread library ?

Answer

Hassan Syed picture Hassan Syed · Jan 14, 2010

semaphores have a synchronized counter and mutex's are just binary (true / false).

A semaphore is often used as a definitive mechanism for answering how many elements of a resource are in use -- e.g., an object that represents n worker threads might use a semaphore to count how many worker threads are available.

Truth is you can represent a semaphore by an INT that is synchronized by a mutex.