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.  
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.

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.
hi thanesh,

have a look at this:

hope it helps :)
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?  
Get your problem seen by more experts

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

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

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
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 ?


Hi jkr,

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

Please forget my above posting.

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.
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.
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

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.