Motivation: reason why I’m considering it is that my genius project manager thinks that boost is another dependency and that it is horrible because “you depend on it”(I tried explaining the quality of boost, then gave up after some time 🙁 ). Smaller reason why I would like to do it is that I would like to learn c++11 features, because people will start writing code in it.
So:
- Is there a 1:1 mapping between
#include<thread> #include<mutex>and
boost equivalents? - Would you consider a good idea to replace boost stuff with c++11
stuff. My usage is primitive, but are there examples when std doesnt
offer what boost does? Or (blasphemy) vice versa?
P.S.
I use GCC so headers are there.
There are several differences between Boost.Thread and the C++11 standard thread library:
std::async, but Boost does notboost::shared_mutexfor multiple-reader/single-writer locking. The analogousstd::shared_timed_mutexis available only since C++14 (N3891), whilestd::shared_mutexis available only since C++17 (N4508).boost::unique_futurevsstd::future)std::threadare different toboost::thread— Boost usesboost::bind, which requires copyable arguments.std::threadallows move-only types such asstd::unique_ptrto be passed as arguments. Due to the use ofboost::bind, the semantics of placeholders such as_1in nested bind expressions can be different too.join()ordetach()then theboost::threaddestructor and assignment operator will calldetach()on the thread object being destroyed/assigned to. With a C++11std::threadobject, this will result in a call tostd::terminate()and abort the application.To clarify the point about move-only parameters, the following is valid C++11, and transfers the ownership of the
intfrom the temporarystd::unique_ptrto the parameter off1when the new thread is started. However, if you useboost::threadthen it won’t work, as it usesboost::bindinternally, andstd::unique_ptrcannot be copied. There is also a bug in the C++11 thread library provided with GCC that prevents this working, as it usesstd::bindin the implementation there too.If you are using Boost then you can probably switch to C++11 threads relatively painlessly if your compiler supports it (e.g. recent versions of GCC on linux have a mostly-complete implementation of the C++11 thread library available in
-std=c++0xmode).If your compiler doesn’t support C++11 threads then you may be able to get a third-party implementation such as Just::Thread, but this is still a dependency.