10

What are the tradeoffs b/w boost::thread, std::thread (C++11), and pthread for high CPU throughput (read: lots of floating point operations) Linux based applications? When should one implementation be used over the others?

The use case here is to call a routine on a buffer (or pointer to a buffer) of contiguous memory, do some work, and return -- in a multithreaded implementation.

BigBrownBear00
  • 1,378
  • 2
  • 14
  • 24
  • 3
    I would say, there should be no visible difference among those three (if i'm right, std::thread comes from boost::thread). The first two also rely on pthread on platforms which support pthread. The only case where pthread could have an advantage would be if using some pthread-specific features not supported by std/boost::thread. – StPiere Sep 02 '20 at 12:01
  • 2
    Unless you continuously create lots of threads and use synchronization primitives a lot, there's no performance difference. Floating-Point performance is not affected by the threading API. `std::thread` has the advantage of being in the standard library and being portable, `boost::thread` has some extra features, and `pthread` is only available on POSIX. – Erlkoenig Sep 02 '20 at 12:02
  • for `lots of floating point operations` it would be better to use alternatives like `omp`, `eigen`, `OpenCV`, `OpenCL` ... depending on application. For example using `OpenCL` you can move parallel operation to video card which currently have lots of floating point power (lots of more then CPU). – Marek R Sep 02 '20 at 12:25
  • 1
    Too many people think of threads in 'adversarial' terms, like separate processes, instead of cooperative terms. Concepts like cancellation are a mistake, that `std::thread` avoided. Detached threads are another mistake. Both of these make deterministic execution and resource unwinding (RAII) very difficult. I can't think of a good reason not to use C++ standard thread facilities if available. – Brett Hale Sep 02 '20 at 12:36

2 Answers2

20
  • std::thread
    • Pro: Is standard; guaranteed to be on all conforming platforms.
    • Con: Requires C++11, so it cannot be used with ancient compilers. Only basic, lowest common denominator features. However, platform specific features can still be used through std::thread::native_handle.
  • boost::thread
    • Pro: Is cross platform, is supported on ancient compilers.
    • Con: Is not standard; requires an external dependency. Similar feature set as standard threads.
  • pthread:
    • Pro: Has more features such as schduling policy.
    • Con: Is only on POSIX systems, which excludes Windows. Non-RAII interface.

When should one implementation be used over the others?

std::thread is often a good default. If you need features of pthread that are not in the standard, you can use them with the help of std::thread::native_handle (with the implications on the portability that come with it). There's no reason to use pthread directly otherwise (that I know of) in C++.

boost::thread can be used if you need ancient pre-C++11 support, to remain portable to other systems.


Note that std::thread itself doesn't need to be used directly. The standard has useful abstractions such as std::reduce, std::packaged_task, std::async, parallel execution policies for algorithms etc.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Additional con of pthreads is that, AFAIK, it has no native C++ API. – Daniel Langr Sep 02 '20 at 12:11
  • Note that the portability issues with `std::thread::native_handle` go deeper than just the OS. According to the C++ standard, that name isn't required to exist; if it does exist, its meaning is entirely implementation defined. – Pete Becker Sep 02 '20 at 14:18
  • `pthread_self` could be used instead, but that works only within the thread. Implementation defined is good though. If the implementation defines it to return what you need, then you're good to go. If it doesn't, then it probably doesn't use pthreads in the first place. – eerorika Sep 02 '20 at 14:23
  • In fairness, pretty much any implementation on POSIX will use pthreads underneath, and `std::thread::native_handle` will, in fact, be the pthreads handle, with all the operations that can be done on that. – Pete Becker Sep 02 '20 at 14:26
5

The only standard-supported one is std::thread and you should use that if your build tools allow C++11 or higher. It's a derived but standardized version of boost::thread.

Pthreads are a platform-specific implementation of threading, std::thread is guaranteed by the standard as per C++11. Usually on POSIX like systems std::thread uses pthreads internally.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122