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

Mutex lock in C++ destructor causes exception when exit() called in Python

I have an application that loads a DLL with a class that handles jobs in a queue. In order to keep it thread safe, a mutex is locked whenever the queue is modified. When the application exits and the destructor is called, the mutex is locked in…
SFBA26
  • 870
  • 3
  • 12
  • 24
2
votes
3 answers

Attempting to reference a deleted function, structure with mutex member

Here's my problem. I have a structure like this. struct threadInfo { std::condition_variable cv; std::mutex m; int priorityLevel; }; When building my code I get this error Error C2280 threadInfo::threadInfo(const threadInfo &):…
lhbortho
  • 167
  • 2
  • 16
2
votes
2 answers

How am I supposed to initialize pthread mutexes?

#include pthread_mutex_t* mut; int main() { mut = PTHREAD_MUTEX_INITIALIZER; } The above code spits out error: expected expression before ‘{’ token. #include pthread_mutex_t* mut = PTHREAD_MUTEX_INITIALIZER; int…
user4385532
2
votes
1 answer

Using a Mutex to Limit the Number of Threads Running at a Time to 2

I have a program that pushes 10 threads into a vector, each of which is supposed to print out a character 5 times before finishing ('A' for the first thread, 'B' for the second, etc). I'm able to get them to either run all at once (using detach())…
gmooney8
  • 75
  • 5
2
votes
1 answer

No deadlock unless linked to pthreads?

Why is it that creating a std::mutex deadlock will not actually cause a deadlock unless the program is linked to pthreads? The following will deadlock when linked with pthreads library and will not deadlock if pthreads is not linked in. Tested on…
A-n-t-h-o-n-y
  • 410
  • 6
  • 14
2
votes
1 answer

Mutex is lock but other threads are entering in critical section

We have studied that if we dealing with multi-threaded problems then we use one of thread synchronizing method called mutex which allows to lock critical section so that other threads do not interfere and goes into block state until mutex unlocks…
suleman
  • 65
  • 12
2
votes
2 answers

Deadlock simulation using std::mutex

I have following example: template class container { public: std::mutex _lock; std::set _elements; void add(T element) { _elements.insert(element); } void remove(T element) { …
Amadeusz
  • 1,488
  • 14
  • 21
2
votes
1 answer

Ajax/JQuery database access/mutex

I have created an office scheduling program that uses jQuery to post to a PHP file which then inserts an appointment into a pgSQL database. This has not happened yet but I can foresee this problem in the future--two office workers try to schedule an…
busbina
  • 549
  • 1
  • 7
  • 19
2
votes
2 answers

Mutex locking between multiple threads

I am a chip designer and we use mutex in our circuits all the time for atomic memory accesses. I am learning to code in CPP and I am having hard time understanding how the Mutex works in CPP. I understand that any program is a process and threads…
paul_brian
  • 31
  • 1
  • 3
2
votes
0 answers

Mutex algorithm for a simple key-value storage

Is there an algorithm to allow reliable mutex using a key-value storage that only supports set() and get() operations plus current time check? Some service API offers in-memory storage with only simple set(key, value) and get(key) methods. I learned…
Serge
  • 1,531
  • 2
  • 21
  • 44
2
votes
4 answers

Segfault occuring in attempt to synchronize queue

I am learning about multithreading and I wanted to simulate producer-consumer problem ( using semaphore if I can call it that ). I have a class that holds a queue, producer push ints into queue and consumer retrieves it and prints it. I simulated is…
Darlyn
  • 4,715
  • 12
  • 40
  • 90
2
votes
2 answers

How to flush a log message and unlock a mutex automatically in C++?

I wrote this logger. It works fine, but there is something I have not been able to do. uLOG(warning) << "Test log message " << 123 << uLOGE; uLOG locks a C++11 mutex and starts writing on a file stream. uLOGE flushes the stream and unlocks the…
Pietro
  • 12,086
  • 26
  • 100
  • 193
2
votes
1 answer

Relationship of unique lock, mutex and condition variable in c++

Hello I have the following code: // condition_variable example #include // std::cout #include // std::thread #include // std::mutex, std::unique_lock #include //…
Redis1001
  • 157
  • 15
2
votes
0 answers

pthread_mutex_trylock: EBUSY on unused mutex?

Thread is locking up on me. A trylock is performed first, followed by a blocking lock (this is mostly done so I can log if something else has the lock at this point). The trylock is failing, and we block forever on the pthread_mutex_lock(). Thing…
async_fm
  • 171
  • 1
  • 2
  • 7
2
votes
2 answers

ARM Cortex-M4 Mutex Lock. DMB Instruction

I read following document: Barrier_Litmus_Tests_and_Cookbook by ARM. Section 7.2 shows the code of acquiring a mutex/semaphore. Loop LDREX R5, [R1] ; read lock CMP R5, #0 ; check if 0 STREXEQ R5, R0, [R1] ; attempt to store new value …
GNA
  • 479
  • 3
  • 18