我想要做的是从每个线程中返回一个值。但是,它会出现这个异常信息。
代码看起来像这样。
而 MyFunction 看起来像这样。
libc++abi.dylib: terminating with uncaught exception of type std::__1::future_error: Operation not permitted on an object without an associated state.
代码看起来像这样。
vector<thread> t;
promise<class_name> promises;
vector<future<class_name>> futures;
for(int i = 0; i < NumberOfThreads; i++)
{
futures.push_back(promises.get_future());
t.push_back(thread(MyFunction ,i , pointList, std::move(promises)));
}
而 MyFunction 看起来像这样。
void MyFunction(int index, const vector<Point>& pointList, promise<class_name>&& p)
{
....
p.set_value(classObj);
}
如果我使用一个线程,那么它可以正常运行,没有异常消息。
有什么解决问题的想法吗?