std :: vector,线程安全,多线程 [英] std::vector, thread-safety, multi-threading
问题描述
我在多线程应用程序中将std :: vector用作共享数据.我将线程封装在一个类中,例如
I am using std::vector as shared data in a multi-threaded application. I encapsulate the thread inside a class, e.g.,
class ABC {
public:
double a, b, c;
};
boost::mutex mutex1;
class XYZ {
public:
XYZ(vector<ABC> & pVector) {
ptrVector = &pVector;
m_thread = boost::thread(&XYZ::Start, this);
}
~XYZ() {}
void Start();
public:
vector<ABC> * ptrVector;
boost::thread m_thread;
};
void XYZ::Start() {
try {
while(1) {
boost::this_thread::interruption_point();
for (unsigned int i=0; i<ptrVector->size(); i++) {
{
boost::mutex::scoped_lock lock(mutex1);
ptrVector->at(i).a = double(rand())/10000;
ptrVector->at(i).b = double(rand())/10000;
ptrVector->at(i).c = double(rand())/10000;
}
}
}
}
catch(boost::thread_interrupted) {}
catch(std::exception) {}
}
当我关闭应用程序时,有时有时在调试中会出现2条错误消息,有时有时没有错误消息.我经常听到人们谈论std :: vector不是线程安全的,这是其中一种情况吗?我正在使用Visual Studio 2008,升压线程,向量的大小是固定的.谁能提供一些关于如何在多线程应用程序中使用std :: vector的建议.
When I close the application, sometimes, in the debug, there will be 2 error messages, sometimes there will be no error messages. I often heard people talking about std::vector being not thread-safe, is this one of the cases? I am using Visual Studio 2008, boost thread, the size of the vector is fixed. Can anyone also offer some advice on how to use std::vector in a multi-threaded application.
- ETP.exe中0x7688b9bc的首次机会异常:Microsoft C ++ 异常:内存位置0x02d8f7bc的std :: out_of_range..
- ETP.exe中0x00e916e0处的首次机会异常:0xC0000005:访问 违反读取位置0x00000008.
- 第二次机会断言失败:文件c:\ program files (x86)\ Microsoft Visual Studio 9.0 \ vc \ include \ vector,第二行 机会断言失败:文件c:\ program files(x86)\ microsoft 视觉工作室9.0 \ vc \ include \ vector98
- First-chance exception at 0x7688b9bc in ETP.exe: Microsoft C++ exception: std::out_of_range at memory location 0x02d8f7bc..
- First-chance exception at 0x00e916e0 in ETP.exe: 0xC0000005: Access violation reading location 0x00000008.
- Second Chance Assertion Failed: File c:\program files (x86)\microsoft visual studio 9.0\vc\include\vector, Line Second Chance Assertion Failed: File c:\program files (x86)\microsoft visual studio 9.0\vc\include\vector98
谢谢.
推荐答案
实际上,绝对毫无意义,指出状态X
是否是线程安全的!您需要符合什么样的用途.例如,当一个类以某种方式在一个线程中使用而在另一个线程中销毁时,几乎没有任何类是线程安全的".
Actually, it is absolutely pointless to state X
is or is not thread-safe! You need to qualify for what kind of uses. For example, hardly any class will be "thread-safe" when it is somehow used in one thread and destroyed in another.
也就是说,std::vector<T>
不是线程安全的,独立于重复的频率的说法是错误.但是,似乎大多数人既不理解也不欣赏给出的线程安全性保证. std::vector<T>
在以下意义上是线程安全的:
That said, the statement that std::vector<T>
is not thread- safe, independent of how often it is repeated, is wrong. However, it seems most people neither understand nor appreciate the thread-safety guarantees given. std::vector<T>
is thread-safe in the following sense:
- 您可以同时从多个线程读取矢量对象.
- 如果有一个线程在更改矢量对象,则不应同时存在读取器或写入器.
- 对矢量对象的访问不会干扰其他矢量对象.
这适用于向量结构本身.对包含对象的访问受任何强加于它们的规则的约束.这些显然不是许多人考虑到的线程安全性保证,但是任何更强大的功能都无法与容器接口一起使用.
This applies to vector structure itself. Accesses to contained object are bound to whatever rules are imposed on them. These are apparently not the thread-safety guarantees many people have in mind but anything stronger won't work with the container interface.
这篇关于std :: vector,线程安全,多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!