C++ 11 Thread vs Boost Thread is there any differe

2019-04-20 03:23发布

问题:

This question already has an answer here:

  • Is it smart to replace boost::thread and boost::mutex with c++11 equivalents? 6 answers

What are the advantages/disadvantages of using the C++11 multithreading classes versus the ones found in Boost? I will only be using Linux so I do not require portability. Is there a lack of features in one of the libraries? Any known limitations? Better syntax?

回答1:

Standard threads have the advantage of being standardised, therefore portable to any compliant implementation.

The Boost thread library is more or less identical; the standard library was based on that library, and there has been an effort to make Boost a conformant implementation of the standard. It has a few extensions which might be useful, including:

  • join with timeout
  • thread interruption
  • thread groups
  • extra lock types


回答2:

In general, boost classes are only wrappers around functions/objects that exist in given OS. Their main advantage is that boost contains versions written for most operating systems, hence the wrapper provides portability the original functions/objects sometimes do not.

If there is nothing else your need from boost I would strongly suggest using standard C++11 threads.

Reasons:

  • boost will not provide more than the system allows for

  • your code will not have any wrapper overhead (however small it may be)

  • boost support for c++11 threads is a new feature and I would fear that it could introduce some errors in the boosts' implementation

  • you will not have to rely on boost libraries and will save yourself time compiling and linking them, etc.

  • you will not have to update boost, because you will not be using it

Of course, boost has some pros also:

  • many people know boost and the code will (possibly) be easier to read

  • if you decide you need to port the code you may have an easier time (though C++11 is standard, so somewhere down the line all compilers will implement it)



标签: c++ boost c++11