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
983
votes
36 answers

Difference between binary semaphore and mutex

Is there any difference between a binary semaphore and mutex or are they essentially the same?
Nitin
  • 15,151
  • 8
  • 23
  • 14
929
votes
10 answers

What is a mutex?

A mutex is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a mutex and how do you use it?
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24
738
votes
39 answers

What is the correct way to create a single-instance WPF application?

Using C# and WPF under .NET (rather than Windows Forms or console), what is the correct way to create an application that can only be run as a single instance? I know it has something to do with some mythical thing called a mutex, rarely can I find…
Nidonocu
  • 12,476
  • 7
  • 42
  • 43
640
votes
10 answers

What is the difference between lock, mutex and semaphore?

I've heard these words related to concurrent programming, but what's the difference between lock, mutex and semaphore?
victor
  • 6,411
  • 3
  • 16
  • 5
405
votes
7 answers

What is a good pattern for using a Global Mutex in C#?

The Mutex class is very misunderstood, and Global mutexes even more so. What is good, safe pattern to use when creating Global mutexes? One that will work Regardless of the locale my machine is in Is guaranteed to release the mutex…
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
375
votes
8 answers

When should one use a spinlock instead of mutex?

I think both are doing the same job,how do you decide which one to use for synchronization?
compile-fan
  • 16,885
  • 22
  • 59
  • 73
265
votes
24 answers

What is the Swift equivalent to Objective-C's "@synchronized"?

I've searched the Swift book, but can't find the Swift version of @synchronized. How do I do mutual exclusion in Swift?
Bill
  • 44,502
  • 24
  • 122
  • 213
219
votes
7 answers

Mutex example / tutorial?

I was trying to understand how mutexes work. Did a lot of Googling but it still left some doubts of how it works because I created my own program in which locking didn't work. One absolutely non-intuitive syntax of the mutex is pthread_mutex_lock(…
Nav
  • 19,885
  • 27
  • 92
  • 135
212
votes
8 answers

Concurrent HashSet in .NET Framework?

I have the following class. class Test{ public HashSet Data = new HashSet(); } I need to change the field "Data" from different threads, so I would like some opinions on my current thread-safe implementation. class Test{ …
kukab
  • 2,133
  • 2
  • 12
  • 4
209
votes
6 answers

How efficient is locking and unlocked mutex? What is the cost of a mutex?

In a low level language (C, C++ or whatever): I have the choice in between either having a bunch of mutexes (like what pthread gives me or whatever the native system library provides) or a single one for an object. How efficient is it to lock a…
Albert
  • 65,406
  • 61
  • 242
  • 386
207
votes
10 answers

Why do pthreads’ condition variable functions require a mutex?

I’m reading up on pthread.h; the condition variable related functions (like pthread_cond_wait(3)) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
205
votes
8 answers

Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex)

POSIX allows mutexes to be recursive. That means the same thread can lock the same mutex twice and won't deadlock. Of course it also needs to unlock it twice, otherwise no other thread can obtain the mutex. Not all systems supporting pthreads also…
Mecki
  • 125,244
  • 33
  • 244
  • 253
185
votes
7 answers

What are the differences between various threading synchronization options in C#?

Can someone explain the difference between: lock (someobject) {} Using Mutex Using Semaphore Using Monitor Using Other .Net synchronization classes I just can't figure it out. It seems to me the first two are the same?
user38834
  • 1,859
  • 3
  • 12
  • 3
162
votes
9 answers

Should a return statement be inside or outside a lock?

I just realized that in some place in my code I have the return statement inside the lock and sometime outside. Which one is the best? 1) void example() { lock (mutex) { //... } return myData; } 2) void example() { lock…
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
160
votes
7 answers

Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?

Motivation: reason why I'm considering it is that my genius project manager thinks that boost is another dependency and that it is horrible because "you depend on it"(I tried explaining the quality of boost, then gave up after some time :( ).…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
2 3
99 100