提高::螺纹和std ::的unique_ptr [英] boost::thread and std::unique_ptr
本文介绍了提高::螺纹和std ::的unique_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是它在某种程度上可能通过一个std ::的unique_ptr作为参数传递给一个boost ::线程的构造函数?如果没有,什么是最好的解决办法?
一个小例子:
//错误:G ++ uniqueptr_thread.cpp -std =的C ++ 0x#包括LT&;&iostream的GT;
#包括LT&;内存和GT;
#包括LT&;升压/ thread.hpp>类TestThread {
上市:
void运算符()(STD ::的unique_ptr< INT> VAL){
性病::法院LT&;< 参数:<< VAL<<的std :: ENDL;
}};INT主(INT ARGC,CHAR *的argv []){ 的std ::的unique_ptr< INT> ptr的(新INT(5)); 提高::线程日(新TestThread()的std ::移动(PTR));}
解决方案
这编译和运行对我来说:
的#include<&iostream的GT;
#包括LT&;内存和GT;
#包括LT&;螺纹>类TestThread {
上市:
void运算符()(STD ::的unique_ptr< INT> VAL){
性病::法院LT&;< 参数:<< * VAL<<的std :: ENDL;
}
};诠释的main()
{ 的std ::的unique_ptr< INT> ptr的(新INT(5)); 的std ::线程日(TestThread()的std ::移动(PTR));
th.join();}
但它必须是C ++ 0x中的模式。我不知道,如果此举提振仿真是不够好,要做到这一点或没有。
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?
A small example:
// 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));
}
解决方案
This compiles and runs for me:
#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();
}
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.
这篇关于提高::螺纹和std ::的unique_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文