Question is simple - what are simple, ready-to-use solutions for mutexes in C++ before C++11 ? I need nothing fancy - just some bit of code to wait for another callback (that may happen anytime) to finish before executing.
I'm running on your everyday PC, i7 and whatnot. No specific performance requirements (simply not too slow), but must do the job of stopping one process and let the other finish and then resume the first one.
Thanks in advance !
(since C++11) The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.
Mutex: Use a mutex when you (thread) want to execute code that should not be executed by any other thread at the same time.
The process in spinlock may not sleep while waiting for the lock. In contrast, the process in mutex may sleep while waiting for the lock. Spinlock disables preemption. On the other hand, mutex supports preemption.
A Mutex is different than a semaphore as it is a locking mechanism while a semaphore is a signalling mechanism. A binary semaphore can be used as a Mutex but a Mutex can never be used as a semaphore.
Boost Synchronization is surely a valid option.
If you don't want to use Boost (based on your question you may find it overkill for your needs), you can write your own c++ class wrapper around the API provided by your target platform (i.e. POSIX thread mutex for POSIX compliant platforms, or Windows Mutexes or Critical Sections on Microsoft Windows OS).
There are surely a lot of such wrapper class already implemented by someone else, i.e. you can try searching on GitHub using the mutex keyword and restricting results for code and C++ language (here are a lot of results).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With