相同类型的STL容器类线程安全吗? [英] Are STL Container Classes Of Same Type Thread Safe?
问题描述
我有一个向量A的线程1,我也有一个向量B在线程2上。
I have a Vector A on Thread 1, I also have a Vector B on Thread 2.
是可以安全地写向向量A和B时间,因为它们是不同的实例化对象,但是类型相同?
Is it safe to write to vectors A and B at the same time, given they are different instantiated objects but of the same type?
如果没有,为什么不同于在线程1上的int c和int d on Thread 2?
If not, why is this different from the writing to an int c on Thread 1 and an int d on Thread 2?
推荐答案
没有标准容器类是线程安全的。如果两个不同的线程访问修改共享资源的相同成员函数,那么你的代码是不安全的。
None of standard container classes are thread-safe. If two different threads access the same member function which modifies the shared resource(s), then your code is not safe.
代码, vectorA
和 vectorB
不是共享的资源,那么你的代码是安全的,假设向量不不包含共享资源本身。
But in your code, vectorA
and vectorB
are not shared resources, then your code is safe, assuming that the vectors do not contain shared resources themselves.
这篇关于相同类型的STL容器类线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!