相关疑难解决方法(0)

在`std :: tuple`上使用`std :: get <I>`保证对`I`的不同值是线程安全的?

让我说我有

std::tuple<T0, T1, T2> my_tuple{x0, x1, x2};
Run Code Online (Sandbox Code Playgroud)

其中T0,T1T2是值类型(即没有混叠是可能的).

只要每个线程访问不同的元素,访问my_tuple元素并从多个线程同时改变它们是否安全std::get

例:

template <typename T>
void process(T& x) { /* mutate `x` */ }

// ...

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();
Run Code Online (Sandbox Code Playgroud)

本能地,我会说它是安全的,my_tuple可以被认为是struct { T0 x0; T1 x1; T2 x2; };......但它是否由标准保证?

c++ multithreading thread-safety c++11 stdtuple

31
推荐指数
1
解决办法
757
查看次数

标签 统计

c++ ×1

c++11 ×1

multithreading ×1

stdtuple ×1

thread-safety ×1