Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::thread vs std::thread vs pthread

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.

like image 640
BigBrownBear00 Avatar asked Mar 05 '26 23:03

BigBrownBear00


2 Answers

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

like image 132
eerorika Avatar answered Mar 07 '26 13:03

eerorika


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.

like image 43
Hatted Rooster Avatar answered Mar 07 '26 12:03

Hatted Rooster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!