Solved

How pthread_mutex_t is different from semaphore?

Posted on 2002-07-15
3
1,070 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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 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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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