Solved

How pthread_mutex_t is different from semaphore?

Posted on 2002-07-15
3
1,063 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 50 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what does "puppet agent -t" do ? 2 227
Problem to run file 9 112
busybox bash/ash - bash -x | grep 19 141
bash file 10 36
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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