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
2 answers

How to use spinlock in linux?

I am a computer science student, I have one assignment asking me to use spinlock to lock a thread and unlock it once its critical section is completed. The difficulty is I googled many times, but I did not find any useful information. For example, I…
ikd29123
  • 21
  • 1
  • 3
2
votes
1 answer

Calling unlock on a mutex associated with a unique_lock causes undefined behavior

I wrote a simple code where I take a unique_lock and unlock the mutex instead of calling unlock on the lock itself. When the first thread enters the critical section and calls my_mutex.unlock(), many other threads enter the critical section…
kwadhwa
  • 129
  • 1
  • 8
2
votes
3 answers

Use mutex or not in a concurrent reading

I am programming in C++ in Linux and I am using pthreads library. I am using mutex to protect some shared variables but I am not sure if in this specific case it is necessary the use of mutex. I have 3 threads. The shared variable is a string…
Julen Uranga
  • 799
  • 1
  • 6
  • 18
2
votes
1 answer

Simple C mutual exclusion implementation in a dual core AMP system

I am working with the Zynq-7000 SoC - developing a dual core (CPU0, CPU1) application. I want to use the shared on-chip memory (OCM) with cache disabled for bidirectional data exchanging between the cores. My idea is to set the data sharing in the…
Steven
  • 21
  • 1
2
votes
4 answers

Multithreading: classical Producer Consumer algorithm

Something I don't get about the classical algorithm for the Producer-Consumer problem (from Wikipedia:) semaphore mutex = 1 semaphore fillCount = 0 semaphore emptyCount = BUFFER_SIZE procedure producer() { while (true) { item =…
bloodcell
  • 601
  • 1
  • 9
  • 23
2
votes
3 answers

What does it mean to lock a resource using pthread_mutex_lock?

I am new to multi-threading and I have extreme difficulty wrapping my head around mutual exclusion. So here is the prototype for pthread_mutex_lock int pthread_mutex_lock(pthread_mutex_t *mutex); The man page says that The mutex object referenced…
posixKing
  • 408
  • 1
  • 8
  • 17
2
votes
2 answers

Tell if any logged on user is running the application about to be installed/uninstalled

My program uses Inno Setup to install/uninstall it. In my application code I create a Global mutex using the CreateMutex Windows API function. Then in my Inno Setup program I have the following…
user6875452
2
votes
1 answer

C - Mutex attributes

Can I create and use only one mutex attribute to initialize multiple recursive mutexes? Or do I have to create one mutex attribute for each mutex I want to create? Basically I the following code correct? int err; int bufferLength =…
Daniel Oliveira
  • 1,280
  • 14
  • 36
2
votes
1 answer

Deallocating object that holds mutex while locking

I ran into a problem with mutexes that are stored in instances. To give an example, I wrote this: #include #include #include #include #include "Sleep.h" struct Test { std::shared_mutex mutex; }; Test*…
obsilp
  • 296
  • 2
  • 7
2
votes
0 answers

std::mutex: Strategy to avoid deadlock when accessing multiple elements at once

Consider the following example code: #include #include #include #include #include #include #include int main() { constexpr std::size_t num_mutices = 3; constexpr std::size_t…
Ace7k3
  • 424
  • 4
  • 10
2
votes
1 answer

sync threads to read different resources at exactly the same time

I have two cameras and this is important to read the frames with OpenCV exactly at the same time, I thought something like Lock but I cannot figure out the way I can implement this. I need some trigger to push and enable the threads to read frames,…
dariush
  • 3,191
  • 3
  • 24
  • 43
2
votes
1 answer

Are there any platforms that do not support reentrent mutex's or recursive locks?

I'm wondering if my implementation should expect that reentrant mutex's are supported or not. The code is supposed to portable/platform independent. I'm wondering if mutex recursion is common enough that it shouldn't be a concern.
kvn
  • 1,295
  • 11
  • 28
2
votes
1 answer

How to use std::mutex in different threads?

How to properly write multithreaded code with mutex: std::mutex m, m2; ... thread m2.lock(); if ((++reference) == 1) m.lock(); m2.unlock(); ... differenet thread m2.lock(); if ((reference--) == 0) m.unlock(); // error here m2.unlock…
Alexey
  • 593
  • 1
  • 5
  • 14
2
votes
3 answers

boost::scoped_lock RAII behaviour

From a Container class, I'd like to lock a vector of boost::mutex, each one owned by a Controlled instance (weird code design, but for MWE purpose only). // std::vector _vData; void Container::main_method() { for (int i=0;…
Patrizio Bertoni
  • 2,582
  • 31
  • 43
2
votes
6 answers

How can I synchronize three threads?

My app consist of the main-process and two threads, all running concurrently and making use of three fifo-queues: The fifo-q's are Qmain, Q1 and Q2. Internally the queues each use a counter that is incremented when an item is put into the queue, and…
slashmais
  • 7,069
  • 9
  • 54
  • 80