lundi 28 novembre 2016

Is using `std::get` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?

Let's say I have

std::tuple<T0, T1, T2> my_tuple{x0, x1, x2};

where T0, T1 and T2 are value types (i.e. no aliasing is possible).

Is it safe to access my_tuple's elements and mutate them concurrently from multiple threads using std::get, as long as every thread accesses a different element?

Example:

template <typename T>
void process(T&);

// ...

std::thread{[&]{ process(std::get<0>(my_tuple)); }}.detach();
std::thread{[&]{ process(std::get<1>(my_tuple)); }}.detach();
std::thread{[&]{ process(std::get<2>(my_tuple)); }}.detach();

Instinctively I would say it is safe, as my_tuple can be thought of as struct { T0 x0; T1 x1; T2 x2; };... but is it guaranteed by the standard?

Aucun commentaire:

Enregistrer un commentaire