C++ multiple return values from thread using promise, future?

Viewed 3614

What I want to do is to return a value from each thread. However, it has this exception message.

libc++abi.dylib: terminating with uncaught exception of type std::__1::future_error: Operation not permitted on an object without an associated state.

The code looks like this.

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)));
}

and the MyFunction looks like this.

void MyFunction(int index, const vector<Point>& pointList, promise<class_name>&& p)
{
....
p.set_value(classObj);
}

If I use a thread, then it works fine without exception message.

Any idea to solve this problem?

2 Answers
Related