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

前端 未结 2 425
無奈伤痛
無奈伤痛 2020-12-30 20:16

Let\'s say I have

std::tuple my_tuple{x0, x1, x2};

where T0, T1 and T2 are value

2条回答
  •  猫巷女王i
    2020-12-30 21:06

    The short answer is that it depends on the types and what does process do instead of get. By itself, get merely retrieve the address of the object and return it as a reference. Retrieving the address is mostly just reading the contents of integers. It does not raise race conditions. Roughly speaking, the code snippet in your question is thread-safe if and only if the following is thread-safe,

    T1 t1;
    T2 t2;
    T3 t3;
    
    std::thread{[&]{process(t1);}}.detach();
    std::thread{[&]{process(t2);}}.detach();
    std::thread{[&]{process(t3);}}.detach();
    

提交回复
热议问题