Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How pthread_mutex_t is different from semaphore?

Posted on 2002-07-15
3
Medium Priority
?
1,073 Views
Last Modified: 2012-05-04
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
Comment
Question by:nakya
3 Comments
 
LVL 5

Accepted Solution

by:
BlackDiamond earned 200 total points
ID: 7157666
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
 
LVL 20

Expert Comment

by:jmcg
ID: 9925563
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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

971 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