15

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!

Community
  • 1
  • 1
user271077
  • 996
  • 1
  • 19
  • 35
  • I believe the std::thread implementation is based on the boost::thread implementation, so there shouldnt be major differences. – Brady Sep 11 '12 at 08:05
  • `boost::thread` is quite different from `std::thread`. They are not interchangeable. `boost::thread` was designed several years ago, and it shows its age. – Pete Becker Sep 11 '12 at 09:41
  • hi, you should have mentioned if you use VS or not(and if so 2010 or 2012)... btw you might wanna read this: http://akrzemi1.wordpress.com/2012/11/14/not-using-stdthread/ – NoSenseEtAl Jan 01 '13 at 04:23

3 Answers3

17

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.

EddieBytes
  • 1,333
  • 9
  • 20
  • 2
    I have found that one reason to prefer the version with your compiler's standard library above the boost one is that the compiler's one can be more optimised for the platform where boost has to be compatible across many platforms and vendors. One case in point is `shared_ptr<>` -- the boost one pulls in a huge number of headers where the Visual Studio 2008 one pulled in only a few, which had a marked positive effect on our build times when we switched to it. – the_mandrill Sep 11 '12 at 08:32
  • 1
    Do you mean that std::thread wouldn't increase the binary size? I'm afraid that's incorrect assumption. – Igor R. Sep 11 '12 at 09:31
  • @IgorR. indeed you are correct, I have removed the statement from my answer as it was misleading – EddieBytes Sep 11 '12 at 11:46
  • @the_mandrill note that in VS2008 `shared_ptr` was buggy and awfully implemented! The following (legitimate, as per Standard) code would crash there: struct a { a() : p_(this, mem_fn(&a::delete_me)) {} void reset() { p_.reset(); } void delete_me() { delete this; } shared_ptr p_; }; int main() { a* p = new a; p->reset(); } – Igor R. Sep 11 '12 at 12:18
  • Ok, I never ran into problems with that myself. I think my point still stands though. I'm not necessarily talking about increasing the binary size, just that there _may_ be some benefits (eg compile time, performance) of using the compiler's standard library over a more portable implementation such as boost – the_mandrill Sep 11 '12 at 13:17
3

Keep in mind that Boost.Thread is a portable library and compiles on a wide range of platfomrs/compilers - including those, where std::thread is unavailable.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
0

It really depends on your habits and preferences.. With boost you have a whole set of libraries that make your life easier, but they will need to be installed on the system your program is compiled on while with std threads all you need is a cpp compiler.

Gung Foo
  • 13,392
  • 5
  • 31
  • 39
  • 7
    For `std::thread` you need specifically a C++11 compiler. All the so-called C++11 compilers in existence are part-complete and in constant change, so it's not necessarily a no-brainer to use one. – Steve Jessop Sep 11 '12 at 09:46