Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1078
  • Last Modified:

How pthread_mutex_t is different from semaphore?

What is the exact difference between pthread_mutex_t and semaphore? What are the implementation level differences between both?
Can anybody elaborate it further?
0
nakya
Asked:
nakya
1 Solution
 
BlackDiamondCommented:
The difference between a mutex and a semaphore is how the blocking is handled.  Think of a semaphore as simply a counter.  This counter is created in the kernel memory space, and is available to be accessed by multiple users and processes.  This is useful in that you can have different users or applications, or multiple instances of the same application running that need to access the same resources.  When they need to access the resource, the application will need to check the semaphore. The semaphore will be incremented (or decremented depending on the implementation).  When the semaphore value reaches zero (usually) the resource is "available" and another process can increment/decrement the semaphore and use the resource, then set the semaphore back.

A mutex (mutual exclusion) is generally used to block threads that are initiated by the same calling process.  Same principle as a semaphore, but used to protect resources (including variables) in your multi-threaded app.

So, for example, if you have a multithreaded application that has an open file handle to a log file, and you have a function or method that writes to the log file, you do not want 2 of your threads trying to write to the same log file at the same time.  So at the beginning of the function that writes to the file, you would set a mutex (call pthread_mutex_lock with posix threads).  The first thread that tries to call that function will be successful (it will successfully create the mutex), and will be able to execute the write function.  If another thread tries to call the write function at the same time, then it will be blocked when it tries to call pthread_mutex_lock, and will continue blocking until pthread_mutex_unlock is called, which you would place at the end of your write function.


Here is a quick example of how to protect a variable from being changed by 2 threads at the same time. (better to check for errors in here, but this is the simplest possible example).

#include <pthread.h>

void incrementMyGlobalCounter(int incVal) {
   static pthread_mutex_t localMutex = PTHREAD_MUTEX_INITIALIZER;

   pthread_mutex_lock(&localMutex);

   myGlobalCounter += incVal;

   pthread_mutex_unlock(&localMutex);
}
0
 
jmcgOwnerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: BlackDiamond {http:#7157666}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
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.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now