Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Using Mutex Vs Semaphore

Posted on 2006-04-07
8
Medium Priority
?
10,827 Views
Last Modified: 2012-08-13
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.  
0
Comment
Question by:thanesh
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16404451
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
 
LVL 20

Expert Comment

by:ikework
ID: 16404453
hi thanesh,

have a look at this:

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


hope it helps :)
ike
0
 
LVL 1

Author Comment

by:thanesh
ID: 16404511
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 16404521
No, both can be used across process boundaries. Only CRITICAL_SECTIONs are limited to a single process.
0
 
LVL 2

Expert Comment

by:bachra04
ID: 16405702
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
 
LVL 2

Expert Comment

by:bachra04
ID: 16405853
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 16411092
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
 

Expert Comment

by:shaminms
ID: 22810628
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question