I've passed a promise as a reference to a thread. Afterwards, the promise was moved into a vector via std::move. This is causing a segmentation fault when the software is executed.
I reckon the reference in the thread is never updated after moving the promise? How can I pass the promise to the thread so that I can move it afterwards? Please see the following code example of my problem.
#include <iostream>
#include <thread>
#include <vector>
#include <future>
class Test {
public:
std::thread t;
std::promise<int> p;
Test(std::thread&& rt, std::promise<int>&& rp) : t(std::move(rt)), p(std::move(rp)) {}
};
int main()
{
std::vector<Test> tests;
{
auto p = std::promise<int>();
std::thread t ([&p]{
std::cout << 1;
p.set_value(1);
});
tests.push_back(Test(std::move(t), std::move(p)));
}
for(Test& mytest : tests)
{
mytest.t.join();
}
}