boost :: thread和std :: unique_ptr [英] boost::thread and std::unique_ptr

查看:280
本文介绍了boost :: thread和std :: unique_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是有可能传递一个std :: unique_ptr作为参数到boost ::线程构造函数?如果没有,最好的解决方法是什么?

Is it somehow possible to pass an std::unique_ptr as a parameter to a boost::thread constructor? If not, what is the best workaround?

一个小例子:

// errors: g++ uniqueptr_thread.cpp -std=c++0x

#include <iostream>
#include <memory>
#include <boost/thread.hpp>

class TestThread{
public:
  void operator()(std::unique_ptr<int> val){
    std::cout << "parameter: " << val << std::endl;
  }

};

int main(int argc, char* argv[]){

  std::unique_ptr<int> ptr(new int(5));

  boost::thread th( new TestThread(), std::move(ptr));

}


推荐答案

并为我运行:

#include <iostream>
#include <memory>
#include <thread>

class TestThread{
public:
  void operator()(std::unique_ptr<int> val){
    std::cout << "parameter: " << *val << std::endl;
  }
};

int main()
{

  std::unique_ptr<int> ptr(new int(5));

  std::thread th( TestThread(), std::move(ptr));
  th.join();

}

但它必须是在C ++ 0x模式。我不知道升压移动仿真是否足以做到这一点或不。

But it has to be in C++0x mode. I don't know if the boost move emulation is good enough to do this or not.

这篇关于boost :: thread和std :: unique_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆