2

There are lots of articles/questions over the internet about the difference of semaphore and mutex, but is it a good approach to use both mutex and semaphore in a thread function?

Something like this (In C)

pthread_mutex_lock(&mutex);
//(Access to critical section)
sem_wait(&sem);

pthread_mutex_unlock(&mutex);
Junior Programmer
  • 310
  • 1
  • 6
  • 20
  • 5
    It's a good approach to use exactly and only what you need to solve the problem at hand. Whatever that is. You haven't told us what that is, or why you think you might need one or the other or both, so your question is essentially meaningless. – user207421 Nov 26 '14 at 09:41
  • 2
    Why have all those articles failed to help you decide? – n. m. could be an AI Nov 26 '14 at 10:48

5 Answers5

2

In a piece of code as you show it, no. As holding mutex will prevent any other threads enering the critical section. Use a semaphore in a producer/consumer setting and mutex when you require mutual exclusion (only one thread executes the critical section).

A related question with few informative answers: When to use Semaphores and when Mutex

Community
  • 1
  • 1
jev
  • 2,023
  • 1
  • 17
  • 26
1

Due to similarity in their implementation a mutex would be referred as binary semaphore. Actually they are different.

  • mutex is locking mechanism used to synchronize access to a resource.
  • semaphore is signaling mechanism

This Link may Clarify your doubt

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Manjunath N
  • 1,365
  • 11
  • 22
0

I used a combination of (windows) mutex and semaphore for an inter thread messaging system in this example windows console program to copy a file. Other than the setup, the messaging functions and the threads are small and simple. By waiting for both a mutex for ownership and for a semaphore to indicate a message is in a queue and to decrement a semaphore with a non-zero count in a single atomic call, then priority between the sending and receive threads is not an issue.

http://rcgldr.net/misc/mtcopy.zip

rcgldr
  • 27,407
  • 3
  • 36
  • 61
0

Yes, for example on a multi producer multi consumer queue implemented with a simple linked list. You use the mutex to protect basic linked list variables, and the semaphore to signal the addition of elements on the queue.

void queue_push(...) {
    lock mutex 
    add element to linked list
    signal sem
    unlock mutex
}

void queue_pop(...) {
start:
    wait on sem
    lock mutex
    if queue is empty: goto start
    remove element
    unlock mutex
    return element
}
vz0
  • 32,345
  • 7
  • 44
  • 77
0

It's bad programming practice to use mutex and semaphores as concurrency primitives in your code. Instead, you should use higher level synchronization and communication primitives and restrict mutex and semaphore usage to implement those higher level types. Examples of high level concurrency types are thread safe queues, CSP channels ("go channels"), promises/futures and event objects:

hdante
  • 7,685
  • 3
  • 31
  • 36
  • The mutex or semaphore has to be _somewhere_. It is not obvious what kind of code the OP is writing or where it is located in their program design. – Lundin Nov 26 '14 at 10:54
  • The example code I linked to accomplishes concurrency by using WaitForMultipleObjects() (for both the mutex and semaphore). – rcgldr Nov 26 '14 at 18:13
  • Lundin, it's obvious to me and it's obvious for the "Junior Programmer" – hdante Nov 27 '14 at 23:53