Using Mutex Vs Semaphore

Posted on 2006-04-07
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.  
Question by:thanesh
    LVL 86

    Expert Comment

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

    Expert Comment

    hi thanesh,

    have a look at this:

    hope it helps :)
    LVL 1

    Author Comment

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

    Accepted Solution

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

    Expert Comment

    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 ?


    LVL 2

    Expert Comment

    Hi jkr,

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

    Please forget my above posting.

    LVL 17

    Expert Comment

    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.

    Expert Comment

    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.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    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…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now