I'm new to C++ (coming from C#) writing a multi-threaded C++ application and wondering what is better to use: std::thread
or its Boost counterpart?
I saw the following discussion in another Stack Overflow post, but still do not have the complete picture of why should I choose one over the other. If not starting a new program, should I use standard library or Boost threads?
Thanks!
Example# std::async is also able to make threads. Compared to std::thread it is considered less powerful but easier to use when you just want to run a function asynchronously.
Since boost is mainly just a wrapper around pthreads (on posix platforms) it helps to know what is going on underneath. In attempting to be generic, boost leaves the platform specific functionality unwrapped. In order to get to it you need to use the native_handle() calls.
Standard C++ 11's threading is based on boost::thread, now it's cross platform and no dependency needed. The code is simple, the thread function will run right after the std::thread is declared.
std::sort could, in principle, use parallel execution when sorting elements of fundamental type (it wouldn't be observable whether it does), but not user-defined type (unless explicitly given permission via execution policy parameter, of course). The type's operator< may not be thread-safe.
If you're not already using boost in your project, there is no reason to use boost::thread in favor of std::thread. That is unless you are using some feature from boost not available in the STL. std::thread is suitable enough for most use cases, and unless compelling arguments are presented, writing standard code is always preferable.
If however you are already using boost in your project, check out if boost::thread offers anything extra compared to std::thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With