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

Not able to force sequential execution of thread in C++? DeadLock?

I am trying to read words from a string thread wise . Means one thread reads one word and when all the words finished then all the threads should also exit peacefully . In this example there are 11 words in the string and 4 threads are operating on…
user3798283
  • 467
  • 3
  • 10
2
votes
3 answers

What difference between C++ std::mutex and windows CreateMutex

I'm programming in Windows on c++ (Visual Studio) I can create mutex using either std::mutex or CreateMutex. What is the difference between them? Which one I should prefer and which one is faster? Do they have any specifics in usage or…
Artem
  • 91
  • 1
  • 4
2
votes
1 answer

Why need a null mutex?

Why would one need a mutex object where the Acquire and release methods just return 0? I am studying the ACE framework and it has a Null_Mutex class, and I was wondering how it would come to use. class Null_Mutex { public: Null_Mutex (void)…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
2
votes
3 answers

Do i need a mutex for a static function?

I have a C++ class with a static function: class Foo { public: static void bar(int &a) { a++; } } EDIT: The variable passed as argument is only used within the calling scope. So it is not accessed by another thread. Do i have…
dab0bby
  • 2,951
  • 1
  • 31
  • 35
2
votes
2 answers

Threading a member function with a std::mutex as an argument

So I have an object with a member function I want to thread. Since this function will be manipulating some resource outside of the object, I want to pass a mutex by reference as an argument to this function: #include #include…
Fat-chunk
  • 361
  • 5
  • 14
2
votes
0 answers

Having trouble understanding mutex usage with threads

I'm running the following code (code at the bottom) to get a feel of how to work with threads and mutex's. And from how I thought mutex's work is that once there is a locked code section marked by pthread_mutex_lock then any other thread cannot…
Kenshin
  • 177
  • 1
  • 9
2
votes
3 answers

Adding a move only type to a class makes that class as a move only type?

In Scott Meyers' "Effective Modern C++", he writes: It's worth nothing that because std::mutex is a move-only type (i.e., a type that can be moved, but not copied), a side effect of adding m to Polynomial is that Polynomial loses the ability to…
RaGa__M
  • 2,550
  • 1
  • 23
  • 44
2
votes
2 answers

C Mutex lock on shared memory accessed by multiple processes

I have two processes that will be running, one that will be reading from shared memory (mmap) and one that will be writing to that shared memory (mmap). These processes are started separately in two different terminals, but they need to be…
2
votes
2 answers

boost named_mutex private member access error

I am suffering from the problem that I need a named_mutex for a managed_shared_memory member within a class and get a "cannot access private member declared in class boost::interprocess::named_mutex" error. However, I both derived my class from…
gilgamash
  • 862
  • 10
  • 31
2
votes
0 answers

How to improve fairness in a program with threads? C++

I have a task to solve the Dining Philosophers problem. I have realised the logic of the program. It is correct based on tests. However with a great number of philosophers (>2000 threads) and for a short work time (5 sec) it's becoming unfair. The…
Acapello
  • 127
  • 9
2
votes
2 answers

Trade off between a recursive mutexes V a more complex class?

I've heard that recursive mutexs are evil but I can't think of the best way to make this class work without one. class Object { public: bool check_stuff() { lock l(m); return /* some calculation */; } void do_something()…
Eoin
  • 1,709
  • 11
  • 22
2
votes
1 answer

Are atomic operations enough for a mutex?

Are just atomic operations enough to implement a mutex in x86. I am asking this in relation to out of order execution. Except atomic access to the integer that specifies whether the mutex is locked or not, are there any additional actions that must…
Abomin
  • 45
  • 4
2
votes
1 answer

Am I supposed to unlock mutexes in a cleanup handler?

A couple of quotes from the manual. Quoting man 3 pthread_mutex_unlock: None of the mutex functions is a cancellation point, not even pthread_mutex_lock, in spite of the fact that it can suspend a thread for arbitrary durations. This way, the…
user4385532
2
votes
2 answers

Mutex with priority in JAVA

I need to build what I call an "Unfair Semaphore" with priority. For example : When a thread with priority = 1 wants to acquire the semaphore, it just has to wait until the other thread with the same priority finished, then it can acquire(). But…
Vanpourix
  • 185
  • 7
2
votes
0 answers

std::mutex::try_lock hangs when called from a static object constructor in a DLL

This happens with the MSVC 2013 compiler. I have a solution with 2 projects: .exe and .dll. The .exe source code: extern "C" int test_dll(); int main() { test_dll(); return 0; } The .dll source code: #include struct AAAA { …
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335