pthreads mutex vs semaphore

后端 未结 9 1068
旧巷少年郎
旧巷少年郎 2020-12-12 11:31

What is the difference between semaphores and mutex provided by pthread library ?

相关标签:
9条回答
  • 2020-12-12 11:49

    I am going to talk about Mutex vs Binary-Semaphore. You obviously use mutex to prevent data in one thread from being accessed by another thread at the same time.

    (Assume that you have just called lock() and in the process of accessing a data. This means that, you don’t expect any other thread (or another instance of the same thread-code) to access the same data locked by the same mutex. That is, if it is the same thread-code getting executed on a different thread instance, hits the lock, then the lock() should block the control flow.)

    This applies to a thread that uses a different thread-code, which is also accessing the same data and which is also locked by the same mutex.

    In this case, you are still in the process of accessing the data and you may take, say, another 15 secs to reach the mutex unlock (so that the other thread that is getting blocked in mutex lock would unblock and would allow the control to access the data).

    Do you ever allow another thread to just unlock the same mutex, and in turn, allow the thread that is already waiting (blocking) in the mutex lock to unblock and access the data? (Hope you got what I am saying here.)

    As per agreed-upon universal definition,

    • with “mutex” this can’t happen. No other thread can unlock the lock in your thread
    • with “binary-semaphore” this can happen. Any other thread can unlock the lock in your thread

    So, if you are very particular about using binary-semaphore instead of mutex, then you should be very careful in “scoping” the locks and unlocks, I mean, that every control-flow that hits every lock should hit an unlock call and also there shouldn’t be any “first unlock”, rather it should be always “first lock”.

    0 讨论(0)
  • 2020-12-12 11:50

    The Toilet Example

    Mutex:

    Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue.

    "Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."

    (A mutex is really a semaphore with value 1.)

    Semaphore:

    Is the number of free identical toilet keys. For Example, say we have four toilets with identical locks and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

    "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore)."

    Source

    0 讨论(0)
  • 2020-12-12 11:50

    Mutex is like sempaphore with with S=1.

    You can control number of concurrent accesses with semaphore but with mutex only one process at a time can access it.

    See the implemenation of these two below: (all functions are atomic)

    Semaphore:

    wait(S) {
          while (S <= 0 )
             ; // busy wait
          S--;
    }
    
    signal(S) {
          S++;
    }
    

    Mutex:

    acquire() {
          while (!available)
                ; // busy wait
          available = false;
    }
    
    release() {
          available = true;
    }
    
    0 讨论(0)
  • 2020-12-12 11:55

    mutex is used to avoid race condition between multiple threads.

    whereas semaphore is used as synchronizing element used across multiple process.

    mutex can't be replaced with binary semaphore since, one process waits for semaphore while other process releases semaphore. In case mutex both acquisition and release is handled by same.

    0 讨论(0)
  • 2020-12-12 11:58

    Semaphore is more used as flag, for which your really don't need to bring RTOS / OS. Semaphore can be accidentally or deliberately changed by other threads (say due to bad coding). When you thread use mutex, it owns the resources. No other thread can ever access it, before resource get free.

    0 讨论(0)
  • 2020-12-12 12:00

    Mutexes can be applied only to threads in a single process and do not work between processes as do semaphores.

    0 讨论(0)
提交回复
热议问题