【发布时间】:2016-05-18 12:20:41
【问题描述】:
我在运行这个我在网上找到的示例时遇到了一些问题。
void asyncFun(std::promise<int> intPromise) {
int result=5;
try {
// calculate the result
intPromise.set_value(result);
}
catch (...) {
intPromise.set_exception(std::current_exception());
}
}
int _tmain(int argc, _TCHAR* argv[]) {
std::promise<int> intPromise;
std::future<int> intFuture = intPromise.get_future();
std::thread t(asyncFun, std::move(intPromise));
std::cout << "Main thread" << std::endl;
int result = intFuture.get(); // may throw MyException
std::cout << result<<std::endl;
return 0;
}
我得到了:
错误 C2280: 'std::promise::promise(const std::promise &)' : 试图引用已删除的 函数 c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional 1149 1 尝试未来
【问题讨论】:
-
这是个bug,你用的是什么版本的VS?
-
使用 VS 2013,我现在正在安装 2015
-
好的,但是还有另一种解决方法,使用
shared_ptr,请参阅我的答案。
标签: c++ multithreading promise future