4

I read that mutex and binary semaphore are different in only one aspect, in the case of mutex the locking thread has to unlock, but in semaphore the locking and unlocking thread can be different?

Which one is more efficient?

Boolean
  • 14,266
  • 30
  • 88
  • 129
  • possible duplicate of [Difference between binary semaphore and mutex.](http://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex). Also see: [Lock, mutex, semaphore… what's the difference?](http://stackoverflow.com/questions/2332765/), [When should we use mutex and when should we use semaphore](http://stackoverflow.com/questions/4039899/), [In what situation do you use a semaphore over a mutex in C++?](http://stackoverflow.com/questions/2350544/), etc. – Cody Gray - on strike Feb 10 '11 at 04:55

1 Answers1

4

Assuming you know the basic differences between a sempahore and mutex :

For fast, simple synchronization, use a critical section.

To synchronize threads across process boundaries, use mutexes.

To synchronize access to limited resources, use a semaphore.

Apart from the fact that mutexes have an owner, the two objects may be optimized for different usage. Mutexes are designed to be held only for a short time; violating this can cause poor performance and unfair scheduling. For example, a running thread may be permitted to acquire a mutex, even though another thread is already blocked on it, creating a deadlock. Semaphores may provide more fairness, or fairness can be forced using several condition variables.

ayush
  • 14,350
  • 11
  • 53
  • 100
  • By "across process boundaries", do you mean to say synchornizing between two threads created under two different processes? – Pacerier Dec 08 '11 at 15:57