简体   繁体   English

std::thread 是 boost::thread 的替代品吗?

[英]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没有的特性:

  • join with timeout加入超时
  • thread attributes线程属性
  • interruption中断
  • 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 with std::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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM