Solved

How pthread_mutex_t is different from semaphore?

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

10 Experts available now in Live!

Get 1:1 Help Now