Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I prefer std::thread or Boost threads?

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!

like image 797
user271077 Avatar asked Sep 11 '12 07:09

user271077


People also ask

Does STD Async use threads?

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.

Does boost use Pthread?

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.

Is std :: thread cross platform?

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.

Is std :: sort thread safe?

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.


1 Answers

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.

like image 63
EddieBytes Avatar answered Nov 04 '22 22:11

EddieBytes