0

Whats best practice to use: std::thread, native calls, or some library? Is std::thread a 'don't-actually-use-this' convenience, or should it be used whenever threading is needed? Before c++11 came around, it looked like some threading library was best for portability, but now that std::thread is a thing...

What should be used - in terms of industry-standard - in major projects now?

Kurtoid
  • 217
  • 5
  • 15

2 Answers2

2

Rule of thumb:

  • If something has been in the standard library for > X years, use it unless you know it won't fit your needs.
  • If something has been in the standard library for <= X years, and you're writing non-critical code: Same as before - use it.
  • Otherwise:

    • Look for how it got into the standard library (often: Boost);
    • Snoop around for discussions mentioning both of them, posts by authors, etc.;
    • Map feature set / design decision differences;
    • if you still don't have enough information to make a decision, ask here.

In your case, see:

C++ 11 Thread vs Boost Thread is there any difference?

where you'll notice Boost threads have some features not in the standard. If you need those, use the Boost threads, otherwise use std::thread.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • How do you choose X? – Chris Hunt Apr 08 '18 at 21:56
  • 1
    @ChrisHunt: I pray to a little effigy of Bjarne Stroustrup? Seriously, I don't know. Definitely X > 3, so it needs to be in there for more than one cycle of the standard. – einpoklum Apr 08 '18 at 21:58
  • 1
    Yes, the "next standard" is a good criterion. If the next standard has heavily revised the feature, it may be best to avoid it, eg not-so-smart_ptr. – Gem Taylor Apr 09 '18 at 11:03
-2

There is no one answer to rule them all.

TL;DR It depends

If you want best performance(maybe one of the reasons you are using c++) use native calls. You have full control and no overhead from wrapper classes and checks.

If you need portability and support of c++11 is present the implementation in std is a good choice. Most of the time this option would be the easiest to start with and have most resources available.

Currently the boost duplicates the features of the standard. But it is still viable option if a compiler does not support c++11. In the latest few revisions same boost features are copied directly in the standard.(smart pointers for example)

anakin
  • 551
  • 4
  • 12
  • 1
    `std::thread` in particular is an *extremely* thin wrapper (read: one-liner) over Linux `pthread_create` or Windows `_beginthreadex`. I sincerely doubt that Kurtoid can do any better. – Bo Persson Apr 08 '18 at 21:56