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
148
votes
13 answers

When should we use mutex and when should we use semaphore

When should we use mutex and when should we use semaphore ?
Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
132
votes
8 answers

Is there a Mutex in Java?

Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e)…
Noam Nevo
  • 3,021
  • 10
  • 35
  • 49
119
votes
7 answers

Which is more efficient, basic mutex lock or atomic integer?

For something simple like a counter if multiple threads will be increasing the number. I read that mutex locks can decrease efficiency since the threads have to wait. So, to me, an atomic counter would be the most efficient, but I read that…
Matt
  • 1,199
  • 2
  • 8
  • 3
118
votes
6 answers

Example for boost shared_mutex (multiple reads/one write)?

I have a multithreaded app that has to read some data often, and occasionally that data is updated. Right now a mutex keeps access to that data safe, but it's expensive because I would like multiple threads to be able to read simultaneously, and…
kevin42
  • 2,108
  • 3
  • 22
  • 21
118
votes
10 answers

What is mutex and semaphore in Java ? What is the main difference?

What is mutex and semaphore in Java ? What is the main difference ?
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
118
votes
7 answers

Are Mutexes needed in javascript?

I have seen this link: Implementing Mutual Exclusion in JavaScript. On the other hand, I have read that there are no threads in javascript, but what exactly does that mean? When events occur, where in the code can they interrupt? And if there are…
Ovesh
  • 5,209
  • 11
  • 53
  • 73
117
votes
4 answers

Python multiprocessing safely writing to a file

I am trying to solve a big numerical problem which involves lots of subproblems, and I'm using Python's multiprocessing module (specifically Pool.map) to split up different independent subproblems onto different cores. Each subproblem involves…
Big Dogg
  • 2,564
  • 5
  • 21
  • 22
113
votes
5 answers

How should I deal with mutexes in movable types in C++?

By design, std::mutex is not movable nor copyable. This means that a class A holding a mutex won't receive a default move constructor. How would I make this type A movable in a thread-safe way?
Jack Sabbath
  • 1,398
  • 2
  • 9
  • 12
105
votes
4 answers

PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)

Is there any difference between pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; Or pthread_mutex_t lock; pthread_mutex_init ( &lock, NULL); Am I safe enough if I use only the first method ? NOTE: My question mostly refers to very small programs…
Kalec
  • 2,681
  • 9
  • 30
  • 49
102
votes
4 answers

Proper use of mutexes in Python

I am starting with multi-threads in python (or at least it is possible that my script creates multiple threads). would this algorithm be the right usage of a Mutex? I haven't tested this code yet and it probably won't even work. I just want…
Richard
  • 15,152
  • 31
  • 85
  • 111
95
votes
3 answers

Calling pthread_cond_signal without locking mutex

I read somewhere that we should lock the mutex before calling pthread_cond_signal and unlock the mutex after calling it: The pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. It…
B Faley
  • 17,120
  • 43
  • 133
  • 223
90
votes
4 answers

Is it possible to determine the thread holding a mutex?

Firstly, I use pthread library to write multithreading C programs. Threads always hung by their waited mutexes. When I use the strace utility to find a thread in the FUTEX_WAIT status, I want to know which thread holds that mutex at that time. But I…
terry
  • 1,437
  • 4
  • 14
  • 11
90
votes
6 answers

Map with concurrent access

When you use a map in a program with concurrent access, is there any need to use a mutex in functions to read values?
user1243746
84
votes
2 answers

Should mutexes be mutable?

Not sure if this is a style question, or something that has a hard rule... If I want to keep the public method interface as const as possible, but make the object thread safe, should I use mutable mutexes? In general, is this good style, or should a…
Marcin
  • 12,245
  • 9
  • 42
  • 49
84
votes
6 answers

How are mutexes implemented?

Are some implementations better than others for specific applications? Is there anything to earn by rolling out your own?
static_rtti
  • 53,760
  • 47
  • 136
  • 192