LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-06-2010, 05:11 AM   #1
jayadhanesh
Member
 
Registered: Feb 2009
Location: Bangalore
Posts: 61

Rep: Reputation: 15
Mutex v/s Semaphore


Hi,

When do use mutexes and when to use semaphores?

Thanks,
Dhanesh.
 
Old 12-06-2010, 05:36 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
The typical uses are: use a mutex when you want to protect a change of state that must not overlap with a change of state being performed by another task; use a semaphore when you want to signal a change from one task to another.

Of course, there are lots of other ways in which tasks may interact, so it will depend on the structure of your application.

A mutex is typically quite efficient. The drawback is that it can only be unlocked from the task that locked it, and does not easily allow for general counting uses.
 
Old 12-06-2010, 11:52 PM   #3
jayadhanesh
Member
 
Registered: Feb 2009
Location: Bangalore
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks neonsignal!! How about a binary semophore, does the above difference holds good
between a mutex and binary semaphore?
 
Old 12-07-2010, 12:20 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
A binary semaphore is much the same as a mutex. However, there may still be a difference whether the task that locked it is the only one that can unlock it. This is primarily an implementation issue, not a difference in the meaning of the construct.

Historically (and theoretically) it was the semaphore that was the basic building block for interprocess communications.

The mutex provides an efficient implementation of a binary semaphore, which is sufficient for many uses of a semaphore (but has restrictions, as outlined above). If your interest is theory, then just treat the mutex as a binary semaphore.

But if your interest is implementation, then my thinking is that a mutex is preferable over a semaphore wherever it is appropriate - not just for efficiency, but because it is less likely to result in complicated deadlocks. Mutex deadlocks are typically related to the simpler problem of correctly ordering resource acquisition and release.

Last edited by neonsignal; 12-07-2010 at 12:22 AM.
 
  


Reply

Tags
mutex, semaphores


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does pthread_cond_wait need a mutex? fuzzyBuzz Programming 4 06-01-2009 02:16 PM
Mutex new2lunix Programming 1 12-02-2008 08:12 PM
Mutex attributes? estratos Programming 12 12-24-2006 03:54 AM
difference mutex vs. semaphore? Thinking Programming 1 11-16-2005 05:26 AM
Whats the difference between a Mutex and a Semaphore? The_Nerd Programming 3 07-07-2004 09:28 AM


All times are GMT -5. The time now is 12:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration