Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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++?

share|improve this question
up vote 14 down vote accepted

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).

share|improve this answer
is this threading already supported by all common compilers? – stijn Nov 15 '11 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 Nov 16 '11 at 7:08
thanks for clearing that out. And yes I meant the lib, not the compiler ;P – stijn Nov 16 '11 at 8:42

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.

share|improve this answer
or there's OpenMP which is built into the VC compiler. – gbjbaanb Nov 15 '11 at 18:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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