简体   繁体   中英

Mutex in C++ BEFORE C++11

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 !

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 (ie 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, ie you can try searching on GitHub using the mutex keyword and restricting results for code and C++ language ( here are a lot of results ).

You might look not only in Boost, but also into POCO (its Mutex class) or Qt (its QMutex class).

If you target only POSIX pthreads , writing your own mutex class above its standard mutexes is doable and has been done a lot of times.

However, switching to C++11 is probably the best approach in 2017. Notice that recent GCC or Clang/LLVM are supporting many (embedded, or tablet/laptop/desktop/server) processors.

(so it might worth the effort - or money - to get somehow a recent GCC or Clang, perhaps by compiling these compilers from their source code, or paying someone to do that work)

You should explain what computer (processor, operating system if any, available memory) you are targetting. Even if POCO or Qt or Boost can be compiled in C++03 mode, they might not fit on a tiny embedded processor (eg some Arduino).

Use pthreads on posix (most Unices) systems. On Windows, your best bet are CriticalSections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM