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
54
votes
9 answers

Plural form of word "mutex"

What is the correct plural form of the portmanteau mutex. Is it mutexes or mutices?
fmark
  • 57,259
  • 27
  • 100
  • 107
53
votes
3 answers

Monitor vs Mutex

I read that mutex is a semaphore with value 1 (binary semaphore) used to enforce mutual exclusion. I read this link Semaphore vs. Monitors - what's the difference? which says that monitor helps in achieving mutual exclusion. Can someone tell me the…
Zephyr
  • 1,521
  • 3
  • 22
  • 42
52
votes
7 answers

Are mutex lock functions sufficient without volatile?

A coworker and I write software for a variety of platforms running on x86, x64, Itanium, PowerPC, and other 10 year old server CPUs. We just had a discussion about whether mutex functions such as pthread_mutex_lock() ... pthread_mutex_unlock() are…
David
  • 1,023
  • 1
  • 8
  • 16
51
votes
13 answers

WPF Single Instance Best Practices

This is the code I implemented so far to create a single instance WPF application: #region Using Directives using System; using System.Globalization; using System.Reflection; using System.Threading; using System.Windows; using…
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
50
votes
3 answers

How pthread_mutex_lock is implemented

I am just curious to know how functions related to synchronization between threads are implemented inside Unix. For example, what happens when I call pthread_mutex_lock? Are there any pointers in use? A reference to the source code would really…
avd
  • 13,993
  • 32
  • 78
  • 99
50
votes
3 answers

Does pthread_cond_wait(&cond_t, &mutex); unlock and then lock the mutex?

I m using pthread_cond_wait(&cond_t, &mutex); in my program and I m wondering why this function needs as a second parameter a mutex variable. Does the pthread_cond_wait() unlock the mutex at the beginning (beginning of the execution…
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
49
votes
6 answers

std::mutex performance compared to win32 CRITICAL_SECTION

how does the performance of std::mutex compared to CRITICAL_SECTION? is it on par? I need lightweight synchronization object (doesn't need to be an interprocess object) is there any STL class that close to CRITICAL_SECTION other than std::mutex ?
uray
  • 11,254
  • 13
  • 54
  • 74
49
votes
2 answers

mutex.lock vs unique_lock

When should I prefer the first piece of code to the second, and do they have fundamental differences std::mutex mtx; mtx.lock(); ... //protected stuff mtx.unlock(); ... //non-protected stuff mtx.lock(); ... //etc and std::mutex…
Morgan Fouque
  • 643
  • 1
  • 7
  • 17
49
votes
8 answers

Pthread mutex assertion error

I'm encountering the following error at unpredictable times in a linux-based (arm) communications application: pthread_mutex_lock.c:82: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed. Google turns up a lot of references to that…
Dave Causey
  • 13,098
  • 7
  • 30
  • 26
48
votes
2 answers

Using Pointers in a for loop

I'm struggling to understand why I have a bug in my code in one state but not the other. It's been a while since I've covered pointers, so I'm probably rusty! Basically I have a repository structure I'm using to store an object in memory, that has a…
Simon
  • 1,743
  • 1
  • 17
  • 20
47
votes
11 answers

Is using a Mutex to prevent multiple instances of the same program from running safe?

I'm using this code to prevent a second instance of my program from running at the same time, is it safe? Mutex appSingleton = new System.Threading.Mutex(false, "MyAppSingleInstnceMutx"); if (appSingleton.WaitOne(0, false)) { …
Malfist
  • 31,179
  • 61
  • 182
  • 269
45
votes
3 answers

Advantages of using condition variables over mutex

I was wondering what is the performance benefit of using condition variables over mutex locks in pthreads. What I found is : "Without condition variables, the programmer would need to have threads continually polling (possibly in a critical…
Abhi
  • 1,167
  • 2
  • 14
  • 21
44
votes
2 answers

boost scoped_lock vs plain lock/unlock

I'm going to use boost::mutex from boost/thread/mutex.hpp. There are several ways to lock/unlock mutex: with scoped_lock, unique_lock, lock_guard, mutex's member functions ::lock() and ::unlock() and nonmember functions lock() and unlock(). I…
Max
  • 4,792
  • 4
  • 29
  • 32
43
votes
1 answer

Use std::lock_guard with try_lock

Is there a way I can tell std::lock_guard to call try_lock instead of lock when it acquires the mutex? The only way I could think of is to use std::adopt_lock: if (!_mutex.try_lock()) { // Handle failure and return from the…
Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
42
votes
10 answers

Object synchronization method was called from an unsynchronized block of code. Exception on Mutex.Release()

I have found different articles about this exception but none of them was my case. Here is the source code: class Program { private static Mutex mutex; private static bool mutexIsLocked = false; static void Main(string[] args) { …
kyurkchyan
  • 2,260
  • 2
  • 23
  • 37