Using Mutex Vs Semaphore

Hi Experts,

I need to protect some data in a multi-threading/multi-processing enviorment.  What syncronization
mechanism should I use.  What is the difference in using Semaphore vs mutexes.  
LVL 1
thaneshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
The difference between mutexes and semaphores basically is that a mutex only grants access to the secured resource exclusively for *one* thread/instance/request, whereas a semaphore has a pin count that would allow multiple threads/instances/requests.
0
ikeworkCommented:
hi thanesh,

have a look at this:

http://koti.mbnet.fi/niclasw/MutexSemaphore.html


hope it helps :)
ike
0
thaneshAuthor Commented:
Thanks guys, I remember reading somewhere that a mutex only protects within multi-threads and semaphore can be used across different processes.  Is it right?  
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

jkrCommented:
No, both can be used across process boundaries. Only CRITICAL_SECTIONs are limited to a single process.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bachra04Commented:
Hi Jkr,

//  No, both can be used across process boundaries. Only CRITICAL_SECTIONs are limited to a single process.

But Mutexes are used to protect critical section, so I see some conflict in the statement above.

Please correct me if I'm wrong ?

B.T

0
bachra04Commented:
Hi jkr,

If you mean critical section objects that are used to synchronize threads , I understand your point.

Please forget my above posting.

B.T
0
rstaveleyCommented:
If you are in a UN*X environment and therefore don't grok CRITICAL_SECTIONs, and you are using pthreads library (i.e. light weight processes), you are working within the user space, which doesn't cross into other processes.

If you are using System V IPC, your semaphore objects (sem_get etc) are in the kernel space and these can be seen by multiple processes.
0
shaminmsCommented:
Mutex allow only one person at a time to enter CRITICAL_SECTIONs but a  semaphore restricts the number of simultaneous users of a shared resource up to a maximum number.
A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section

A semaphore allows more than one thread to enter CRITICAL_SECTIONs up to MAX, and push remaining threads to queue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.