在多线程调用的情况下STL是否会出错?
一个前辈告诉我说多线程调用下,stl的一些容器可能会出错,从而导致数据错误...
对于多线程也没有研究,不过感觉stl应该很安全的,不知道是否会发生意想不到的情况....
[解决办法]
STL非线程安全的,所以不要在不同线程下调用同一个STL对象操作
[解决办法]
vc8下
When /MT, /MTd, /MD, or /MDd is used, the following thread-safety rules are in effect:
Container Classes and complex
The container classes are vector, deque, list, queue, stack , priority_queue, valarray, map, multimap, set, multiset, basic_string, bitset.
For reads to the same object, the object is thread safe for reading in the following scenarios:
From one thread at a time when no writers on other threads.
From many threads at a time when no writers on other threads.
For writes to the same object, the object is thread safe for writing from one thread when no readers on other threads
For reads to different objects of the same class, the object is thread safe for reading in the following scenarios:
From one thread at a time.
From one thread at a time when no writers on other threads.
From many threads at a time.
From many threads at a time when no writers on other threads.
For writes to different objects of the same class, the object is thread safe for writing in the following scenarios:
From one thread when no readers on other threads.
From many threads.
For example, given objects A and B of the same class, it is safe if object A is being written in thread 1 and B is being read in thread 2.
iostream Classes
Note that reading from a stream buffer is not considered to be a read operation. It should be considered as a write operation, because this changes the state of the class.
For reads to the same object, the object is thread safe for reading in the following scenarios:
From one thread at a time when no writers on other threads.
From many threads at a time when no writers on other threads.
For writes to the same object, the object is thread safe for writing in the following scenarios:
From one thread when no readers on other threads.
From many threads (when accesses are limited to stream buffers).
For reads to different objects of the same class, the object is thread safe for reading in the following scenarios:
From one thread at a time.
From one thread at a time when no writers on other threads.
From many threads at a time.
From many threads at a time when no writers on other threads.
For writes to different objects of the same class, the object is thread safe for writing in the following scenarios:
From one thread when no readers on other threads
From many threads
[解决办法]
自己加线程锁
在多线程调用的情况下STL是否会出错?解决思路
在多线程调用的情况下STL是否会出错?一个前辈告诉我说多线程调用下,stl的一些容器可能会出错,从而导致数据
