在向量中编写线程安全

djW*_*ann 1 c++ multithreading stl vector c++11

可能重复:
STL向量和线程安全

如果我有这段代码:

std::vector<std::vector<double>> a;
std::vector<double> b;

// init a,b....

std::vector<double> c;
parallel_for_each (a.begin(); a.end; [&c, &b] (std::vector<double>& aux) {
   c.push_back(foo(b, aux));
});
Run Code Online (Sandbox Code Playgroud)

在向量中添加这样的元素是线程安全的吗?

NPE*_*NPE 5

不,std::vector不是线程安全的.您必须提供同步.