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 :( ). S开发者_JAVA百科maller 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:
- Boost supports thread cancellation, C++11 threads do not
- C++11 supports
std::async
, but Boost does not - Boost has a
boost::shared_mutex
for multiple-reader/single-writer locking. The analogousstd::shared_timed_mutex
is available only since C++14 (N3891), whilestd::shared_mutex
is available only since C++17 (N4508). - C++11 timeouts are different to Boost timeouts (though this should soon change now Boost.Chrono has been accepted).
- Some of the names are different (e.g.
boost::unique_future
vsstd::future
) - The argument-passing semantics of
std::thread
are different toboost::thread
--- Boost usesboost::bind
, which requires copyable arguments.std::thread
allows move-only types such asstd::unique_ptr
to be passed as arguments. Due to the use ofboost::bind
, the semantics of placeholders such as_1
in nested bind expressions can be different too. - If you don't explicitly call
join()
ordetach()
then theboost::thread
destructor and assignment operator will calldetach()
on the thread object being destroyed/assigned to. With a C++11std::thread
object, 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 int
from the temporary std::unique_ptr
to the parameter of f1
when the new thread is started. However, if you use boost::thread
then it won't work, as it uses boost::bind
internally, and std::unique_ptr
cannot be copied. There is also a bug in the C++11 thread library provided with GCC that prevents this working, as it uses std::bind
in the implementation there too.
void f1(std::unique_ptr<int>);
std::thread t1(f1,std::unique_ptr<int>(new int(42)));
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++0x
mode).
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.
std::thread
is largely modelled after boost::thread
, with a few differences:
- boost's non-copyable, one-handle-maps-to-one-os-thread, semantics are retained. But this thread is movable to allow returning thread from factory functions and placing into containers.
- This proposal adds cancellation to the
boost::thread
, which is a significant complication. This change has a large impact not only on thread but the rest of the C++ threading library as well. It is believed this large change is justifiable because of the benefit.
- The thread destructor must now call cancel prior to detaching to avoid accidently leaking child threads when parent threads are canceled.
- An explicit detach member is now required to enable detaching without canceling.
- The concepts of thread handle and thread identity have been separated into two classes (they are the same class in
boost::thread
). This is to support easier manipulation and storage of thread identity.- The ability to create a thread id which is guaranteed to compare equal to no other joinable thread has been added (
boost::thread
does not have this). This is handy for code which wants to know if it is being executed by the same thread as a previous call (recursive mutexes are a concrete example).- There exists a "back door" to get the native thread handle so that clients can manipulate threads using the underlying OS if desired.
This is from 2007, so some points are no longer valid: boost::thread
has a native_handle
function now, and, as commenters point out, std::thread
doesn't have cancellation anymore.
I could not find any significant differences between boost::mutex
and std::mutex
.
Enterprise Case
If you are writing software for the enterprise that needs to run on a moderate to large variety of operating systems and consequently build with a variety of compilers and compiler versions (especially relatively old ones) on those operating systems, my suggestion is to stay away from C++11 altogether for now. That means that you cannot use std::thread
, and I would recommend using boost::thread
.
Basic / Tech Startup Case
If you are writing for one or two operating systems, you know for sure that you will only ever need to build with a modern compiler that mostly supports C++11 (e.g. VS2015, GCC 5.3, Xcode 7), and you are not already dependent on the boost library, then std::thread
could be a good option.
My Experience
I am personally partial to hardened, heavily used, highly compatible, highly consistent libraries such as boost versus a very modern alternative. This is especially true for complicated programming subjects such as threading. Also, I have long experienced great success with boost::thread
(and boost in general) across a vast array of environments, compilers, threading models, etc. When its my choice, I choose boost.
There is one reason not to migrate to std::thread
.
If you are using static linking, std::thread
becomes unusable due to these gcc bugs/features:
- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590
- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57740
Namely, if you call std::thread::detach
or std::thread::join
it will lead to either exception or crash, while boost::thread
works ok in these cases.
With Visual Studio 2013 the std::mutex
seems to behave differently than the boost::mutex
, which caused me some problems (see this question).
With regards to std::shared_mutex added in C++17
The other answers here provide a very good overview of the differences in general. However, there are several issues with std::shared_mutex
that boost solves.
Upgradable mutices. These are absent from
std::thread
. They allow a reader to be upgraded to a writer without allowing any other writers to get in before you. These allow you to do things like pre-process a large computation (for example, reindexing a data structure) when in read mode, then upgrade to write to apply the reindex while only holding the write lock for a short time.Fairness. If you have constant read activity with a
std::shared_mutex
, your writers will be softlocked indefinitely. This is because if another reader comes along, they will always be given priority. Withboost:shared_mutex
, all threads will eventually be given priority.(1) Neither readers nor writers will be starved.
The tl;dr of this is that if you have a very high-throughput system with no downtime and very high contention, std::shared_mutex
will never work for you without manually building a priority system on top of it. boost::shared_mutex
will work out of the box, although you might need to tinker with it in certain cases. I'd argue that std::shared_mutex
's behavior is a latent bug waiting to happen in most code that uses it.
(1) The actual algorithm it uses is based on the OS thread scheduler. In my experience, when reads are saturated, there are longer pauses (when obtaining a write lock) on Windows than on OSX/Linux.
I tried to use shared_ptr from std instead of boost and I actually found a bug in gcc implementation of this class. My application was crashing because of destructor called twice (this class should be thread-safe and shouldn't generate such problems). After moving to boost::shared_ptr all problems disappeared. Current implementations of C++11 are still not mature.
Boost has also more features. For example header in std version doesn't provide serializer to a stream (i.e. cout << duration). Boost has many libraries that use its own , etc. equivalents, but do not cooperate with std versions.
To sum up - if you already have an application written using boost, it is safer to keep your code as it is instead of putting some effort in moving to C++11 standard.
精彩评论