c++ - std::thread 是 boost::thread 的替代品吗?

标签 c++ boost

我有一个代码库,它使用许多分离的 boost::thread 实例,如下所示

boost::thread th( boost::bind(&myFunc,var1, var2 ));

我可以简单地进行搜索替换并开始使用 std::thread 吗?

我是否也需要用 std::bind 替换 boost::bind 或者那是不必要的?

( 我发现我可以搜索将 boost::shared_ptr 替换为 std::shared_ptr 并将 boost::scoped_ptr 替换为 std::unique_ptr 到目前为止没有任何问题。

我的平台是 Windows 7 上的 Visual Studio 11。

最佳答案

Can I simply do a search replace and start using std::thread?

boost::thread 的当前版本有一些 std::thread 中没有的特性:

  • 超时加入
  • 线程属性
  • 打断
  • yield()sleep()(已弃用)

参见 boost documentation ,它们被标记为 EXTENSION。如果您不使用其中的任何一个,那么它应该是一个直接的替代品。

如评论中所述,旧版本的 boost::thread 在销毁时具有不同的行为。如果线程仍然可以连接,他们将分离线程; std::thread 和当前的 boost::thread 调用 std::terminate

Do I need to replace boost::bind with std::bind too or that is unnecessary?

那不是必须的; thread 可以被赋予任何可调用类型。也根本没有必要使用 bind,因为它有一个可变参数构造函数:

std::thread th(&myFunc, var1, var2);

关于c++ - std::thread 是 boost::thread 的替代品吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13401884/

相关文章:

c++ - 如何从重载解析中删除函数?

c++ - Linux/阿尔萨 : Callback when frame is written to sound card

c++ - boost::flyweight 不适用于类

c++ - 在 cygwin 上使用 cmake 链接到 Boost 文件系统时出错

c++ - 在 Windows 7 中使用 windows.h 在 C++ 中更改文件创建日期

c++ - 使用指向 typedef 固定长度数组的指针执行 memcpy 时是否需要取消引用?为什么或者为什么不?

c++ - 在 C++ 中连接写入参数

c++ - 为什么不能在 c++0x 模式下使用 libc++ 连接这个 boost::program_options 示例?

c++ - 需要 boost :asio async server with receiver and acceptor 的例子

c++ - 在 ref/cref 范围内同时迭代