Semaphore or what should I use?

Sergei Organov osv at javad.ru
Thu Dec 2 15:39:26 CET 2004


Ville Vainio <ville at spammers.com> writes:
> >>>>> "Sergei" == Sergei Organov <osv at javad.ru> writes:
> 
>     Sergei> My answer to OP's question is: use either lock (mutex) or
>     Sergei> semaphore.  I'd probably use semaphore as mutexes are
>     Sergei> usually optimized for the case when contention probability
>     Sergei> is low (i.e., they usually shouldn't be locked for a long
>     Sergei> time).
> 
> Both Mutexes and Semaphores put the thread to sleep, so I don't think
> there will be a big difference.

Yeah, most probably from this point of view there is no difference
(mutexes could be implemented using busy-waiting, but I don't think it's
the case). However, though I don't think it's a real issue in this
particular case either, mutexes can do fancy things with the thread that
has locked the mutex (mutex owner thread), like raising its priority to
the highest one of the threads waiting for the mutex to prevent so
called priority inversion.

There could be other subtle differences between mutex and semaphore
behavior resulting in different performance of the application. The
application correctness shouldn't be affected though.

-- 
Sergei.



More information about the Python-list mailing list