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
2
votes
1 answer

LIghtweight mutex

The citation comes from http://preshing.com/20111124/always-use-a-lightweight-mutex/ The Windows Critical Section is what we call a lightweight mutex. It’s optimized for the case when there are no other threads competing for the lock. To…
user6262188
2
votes
2 answers

Why is std::mutex::unlock() not noexcept

Why is std::mutex::unlock() not noexept? For some reason the standard leaves the behavior undefined when a thread that does not own the mutex calls unlock() on it. What is the justification for doing so? Doesn't this cause std::unique_lock or…
Curious
  • 20,870
  • 8
  • 61
  • 146
2
votes
1 answer

File.Exists() and File Access Synchronization across processes

I have a couple applications running in separate processes that each write to a central xml doc on the harddrive. I am using a named mutex to synchronize access to the file - but get the following exception often: System.IO.IOException: The…
kmfk
  • 3,821
  • 2
  • 22
  • 32
2
votes
1 answer

Efficiency of the multi producer-consumer execution

I was trying to make a code with multi producers and consumers. I have created multi-threads for producer and consumer and used semaphores for synchronization. The code was working fine with single producer and consumer. The problem which I am…
Shivam Mitra
  • 1,040
  • 3
  • 17
  • 33
2
votes
1 answer

Given sufficient memory, are locks unnecessary when there is only a single dedicated writer thread?

For a scenario with multiple reader threads and a single writer thread, where the readers are allowed to read slightly outdated data, I've concocted a lockless control flow as shown below in its most basic form in pseudocode: GLOBAL_ATOMIC_POINTER…
Will
  • 2,014
  • 2
  • 19
  • 42
2
votes
0 answers

Why is this code threadsafe?

For this code it says that it is thread unsafe: /* thread-unsafe function */ int increment_counter() { static int counter = 0; counter++; return counter; } But for below code, it is said to be thread safe: /* pseudo-code…
Tom Xue
  • 3,169
  • 7
  • 40
  • 77
2
votes
0 answers

Scope-based locking wrapper for a class

I'm not too experienced with multithreaded programming, but I've come up with the following and I'm wondering whether there are any obvious problems I've overlooked in my naivety. I have a resource (in my case a drawing surface) that can only safely…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
2
votes
2 answers

Could unlocking mutex before function return increase the concurrency?

As we know, std::unique_lock might release the mutex when it's destructor called. Consider the behavior of the line 8th in the source below, lck.unlock(), my question is whether unlocking mutex before function return increase the concurrency or not?…
Qi W.
  • 706
  • 9
  • 21
2
votes
2 answers

When a mutex unlocks does it notify_all or notify_one?

As I understand it, when I have a collection of threads protected by a std::lock_guard or std::unique_lock over std::mutex and the mutex is unlocked by either explicitly unlocking it or by the lock going out of scope, then the waiting threads are…
Brenton Thomas
  • 618
  • 1
  • 7
  • 17
2
votes
2 answers

Wait for buttonPressed() slot to finish before executing buttonReleased()

I have a QPushButton that performs lengthy actions on pressed() and released() signals. How can I make sure that I finished executing all actions of the buttonPressed() slot, before executing the ones of the buttonReleased() slot? I have tried with…
mimo
  • 2,469
  • 2
  • 28
  • 49
2
votes
1 answer

Safe use of a reference in a map in multi-threaded context

The context I have a class (let's say Foo) managing some centralized ressources as an history in a static map, with an accessor to read them and a function to add new data (there is no way to remove a key) : class Foo { private: static…
Caduchon
  • 4,574
  • 4
  • 26
  • 67
2
votes
0 answers

Change program of count factorial using Mutex C++

I have a program to count factorial, but it works uncorrect. I should change it using Mutex, but I don't understand this feature. // POSIX Threads, example #5-1 #include #include using namespace std; void* fact(void *data)…
NineWasps
  • 2,081
  • 8
  • 28
  • 45
2
votes
2 answers

maximum number of mutexes per process/thread in windows

Is there a limit in the maximum number of mutexes per process/thread in a Asp.net application? Just in case the target operating systems are: Windows XP Pro, server 2003/2008 and Windows 7 in the near future. Usually the website is deployed in an…
mas_oz2k1
  • 2,851
  • 3
  • 34
  • 41
2
votes
1 answer

How to define pthread and mutex lock in C files that depend on each other?

I’m new to pthread and mutex lock. I used them before but in a single file (main.c that creates the threads and locks are in the same file as functions and memory that use the locks). How to define mutex lock in C files that depend on each other?…
Pippi
  • 2,451
  • 8
  • 39
  • 59
2
votes
1 answer

Using Mutex and semaphore

I am confused that what should I use either a mutex or a semaphore in my application,basically my application is a multithreaded server programmed using C and Pthreads. In my application one thread has a dependency over the other i.e one thread…
Ramanujam
  • 239
  • 2
  • 3
  • 10