Questions tagged [mutex]

A mutex ("mutual exclusion") is a mechanism to ensure integrity when the same data or resource is accessed (in particular, changed) concurrently from several threads.

A mutex ("mutual exclusion") is a mechanism to ensure integrity when the same data or resource is accessed (in particular, changed) concurrently from several threads.

Usually when the term mutex is used, it refers to a synchronisation primitive functionally identical to a binary semaphore. As OS-level synchronisation is often rather expensive due to context switching, busy-wait solutions based on atomic operations (e.g. mutex or critical section) exist.

See also:

4640 questions
84
votes
2 answers

C++11: why does std::condition_variable use std::unique_lock?

I am a bit confused about the role of std::unique_lock when working with std::condition_variable. As far as I understood the documentation, std::unique_lock is basically a bloated lock guard, with the possibility to swap the state between two…
lucas clemente
  • 6,255
  • 8
  • 41
  • 61
82
votes
9 answers

pthreads mutex vs semaphore

What is the difference between semaphores and mutex provided by pthread library ?
cppdev
  • 6,833
  • 13
  • 40
  • 45
81
votes
8 answers

When is a condition variable needed, isn't a mutex enough?

I'm sure mutex isn't enough that's the reason the concept of condition variables exist; but it beats me and I'm not able to convince myself with a concrete scenario when a condition variable is essential. Differences between Conditional variables,…
80
votes
2 answers

When or why should I use a Mutex over an RwLock?

When I read the documentations of Mutex and RwLock, the difference I see is the following: Mutex can have only one reader or writer at a time, RwLock can have one writer or multiple readers at a time. When you put it that way, RwLock seems always…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
77
votes
6 answers

System-wide mutex in Python on Linux

Is there any easy way to have a system-wide mutex in Python on Linux? By "system-wide", I mean the mutex will be used by a group of Python processes; this is in contrast to a traditional mutex, which is used by a group of threads within the same…
emchristiansen
  • 3,550
  • 3
  • 26
  • 40
77
votes
8 answers

When to use recursive mutex?

I understand recursive mutex allows mutex to be locked more than once without getting to a deadlock and should be unlocked the same number of times. But in what specific situations do you need to use a recursive mutex? I'm looking for…
jasonline
  • 8,646
  • 19
  • 59
  • 80
76
votes
3 answers

understanding of pthread_cond_wait() and pthread_cond_signal()

Generally speaking, pthread_cond_wait() and pthread_cond_signal() are called as below: //thread 1: pthread_mutex_lock(&mutex); pthread_cond_wait(&cond, &mutex); do_something() pthread_mutex_unlock(&mutex); //thread…
user1944267
  • 1,557
  • 5
  • 20
  • 27
65
votes
3 answers

Differences between Conditional variables, Mutexes and Locks

For example the c++0x interfaces I am having a hard time figuring out when to use which of these things (cv, mutex and lock). Can anyone please explain or point to a resource? Thanks in advance.
Ronny Brendel
  • 4,777
  • 5
  • 35
  • 55
61
votes
10 answers

Why doesn't Mutex get released when disposed?

I have the following code: using (Mutex mut = new Mutex(false, MUTEX_NAME)) { if (mut.WaitOne(new TimeSpan(0, 0, 30))) { // Some code that deals with a specific TCP port // Don't want this to run at the same time in another…
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
59
votes
3 answers

std::mutex vs std::recursive_mutex as class member

I have seen some people hate on recursive_mutex: http://www.zaval.org/resources/library/butenhof1.html But when thinking about how to implement a class that is thread safe (mutex protected), it seems to me excruciatingly hard to prove that every…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
58
votes
6 answers

Synchronising twice on the same object?

I was wondering if in Java I would get any odd behaviour if I synchronise twice on the same object? The scenario is as follows pulbic class SillyClassName { object moo; ... public void method1(){ synchronized(moo) { …
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
57
votes
3 answers

C++11 equivalent to boost shared_mutex

Is there a C++11 equivalent for the boost::shared_mutex. Or another solution to handle a multiple reader / single writer situation in C++11?
Haatschii
  • 9,021
  • 10
  • 58
  • 95
56
votes
4 answers

How do you declare a recursive mutex with POSIX threads?

I am a bit confused on how to declare a recursive mutex using pthread. What I try to do is have only one thread at a time be able to run a piece of code(including functions) but after scepticism I figured out that the use of mutexes would not work…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
56
votes
6 answers

Do I need a mutex for reading?

I have a class that has a state (a simple enum) and that is accessed from two threads. For changing state I use a mutex (boost::mutex). Is it safe to check the state (e.g. compare state_ == ESTABLISHED) or do I have to use the mutex in this case…
Tom
55
votes
4 answers

What does std::thread.join() do?

By definition from C++ reference: Blocks the current thread until the thread identified by *this finishes its execution. So does this mean when using .join(), there's no need to mutex.lock() when that thread calls some function? I'm new to mutual…
Guagua
  • 968
  • 1
  • 9
  • 14