7

We use pthread for thread management in C based systems. pthread is in general compilable by C++ compiler (like g++). However, what are the better ways of abstractions for threads in C++?

Also, for making any system to be working in a multi-threaded system, it is also important to make thread safe. What are the standard libraries that requires alternative (installs) to be thread safe or are they unsafe for multi-threaded environments?

Is smart pointers, templates require special measures to make it safe?

What are the best practices for the thread managements in C++?

2 Answers 2

14

If you use C++11, threading is part of standard library and components where it makes sense like smart pointers are thread-safe (collections generally require you lock them yourself).

If you are using Boost, have a look at boost.thread. It is base for what was standardized in C++11 (most new things in C++11 come from boost).

3
  • 1
    is this threading already supported by all common compilers?
    – stijn
    Commented Nov 15, 2011 at 19:07
  • @stijn: This is a standard C++ library (not compiler) feature and many compilers can be used with multiple standard C++ libraries. The GNU stdlibc++ (that comes with gcc) mostly supports it as does (except atomic) the LLVM libcxx (that comes with clang; clang can also be used with the GNU one). I don't know about Visual Studio 2010, but you can use the Boost version (which is interchangeable) with almost any compiler (no atomic there either though). There is also Apache stdcxx implementation that can be used with most compilers.
    – Jan Hudec
    Commented Nov 16, 2011 at 7:08
  • thanks for clearing that out. And yes I meant the lib, not the compiler ;P
    – stijn
    Commented Nov 16, 2011 at 8:42
5

In addition to what Jan Hudec wrote, if you want to use even higher level of abstraction (which can be a good idea) take a look at Intel Threading Building Blocks. Microsoft has Concurrency Runtime which is also excellent but MS specific.

1
  • 1
    or there's OpenMP which is built into the VC compiler.
    – gbjbaanb
    Commented Nov 15, 2011 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.