A thread is created via a packaged task and a std::vector
is returned.
#include <iostream>
#include <future>
#include <thread>
#include <vector>
std::vector<int> func(int &arg)
{
std::vector<int> v = {1,2,3,4};
arg = 10;
return v;
}
int main()
{
std::packaged_task<std::vector<int>(int &)> pt{func};
auto fut = pt.get_future();
int arg = 0;
std::thread thr{std::move(pt), std::ref(arg)};
auto vec = fut.get();
std::cout << arg << std::endl; // data race here ?
thr.join();
}
The std::future
guarantees that the vector is synchronized with the main
thread (before join
),
but I am not sure about the status of the packaged task argument (which is passed by reference).
So the question is whether there is a data race on arg
?