[英]Is std::thread a drop in replacement for boost::thread?
I have a code base that is using many instances of detached boost::thread
as folllows我有一个代码库,它使用了许多分离的boost::thread
实例,如下所示
boost::thread th( boost::bind(&myFunc,var1, var2 ));
Can I simply do a search replace and start using std::thread
?我可以简单地进行搜索替换并开始使用std::thread
吗?
Do I need to replace boost::bind
with std::bind
too or that is unnecessary?我是否也需要用std::bind
替换boost::bind
或者这是不必要的?
( I found I could do search replace boost::shared_ptr
with std::shared_ptr
and boost::scoped_ptr
with std::unique_ptr
so far without any issues. (我发现我可以用std::shared_ptr
搜索替换boost::shared_ptr
和用std::unique_ptr
替换boost::shared_ptr
boost::scoped_ptr
到目前为止没有任何问题。
My platform is Visual Studio 11 on Windows 7.我的平台是 Windows 7 上的 Visual Studio 11。
Can I simply do a search replace and start using std::thread?我可以简单地进行搜索替换并开始使用 std::thread 吗?
The current version of boost::thread
has a few features that aren't in std::thread
:当前版本的boost::thread
有一些std::thread
没有的特性:
yield()
and sleep()
(deprecated) yield()
和sleep()
(已弃用) See the boost documentation , where they are labelled EXTENSION
.请参阅boost 文档,它们被标记为EXTENSION
。 If you're not using any of those, then it should be a direct replacement.如果您不使用其中任何一个,那么它应该是直接替代品。
As mentioned in the comments, older versions of boost::thread
had a different behaviour on destruction.正如评论中提到的,旧版本的boost::thread
在销毁时有不同的行为。 They would detach the thread if it was still joinable;如果它仍然可以连接,他们会分离线程; std::thread
and the current boost::thread
call std::terminate
instead. std::thread
和当前的boost::thread
调用std::terminate
代替。
Do I need to replace
boost::bind
withstd::bind
too or that is unnecessary?我是否也需要用std::bind
替换boost::bind
或者这是不必要的?
That's not necessary;那是没有必要的; thread
can be given any callable type. thread
可以被赋予任何可调用类型。 It's also not necessary to use bind
at all, since it has a variadic constructor:也根本没有必要使用bind
,因为它有一个可变参数构造函数:
std::thread th(&myFunc, var1, var2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.