C++ std::promise Segmentation Fault
This code creates multiple threads and sends a promise& to them. It segfaults when they call promise::set_value to try and return data back to main.
Can anyone explain why this code produces a segmentation fault?
void workerFunc(promise<long>& prom) {
long number = doInterestingThings();
//SEGFAULT!!!
prom.set_value(number);
return;
}
The thread function. It segfaults at prom.set_value. Doesn't do it if I only create 1 thread.
int main (int argc, char** argv) {
vector<promise<long>> promises;
vector<future<long>> futures;
vector<thread> workers;
Intialization.
const int createThisMany = 6;
for (int i = 0; i < createThisMany; i++) {
promises.emplace_back();
futures.push_back(promises.back().get_future());
workers.emplace_back(workerFunc, std::ref(promises.back()));
}
Creates all the threads and promise and future objects. Not included is the main loop where the vectors are monitored and the dead threads removed, etc.
Do promises and futures have to be synchronized, perhaps?
I'm using gcc 4.9 on Lubuntu 14.04