STL 线程安全性
2012-03-30 14:58
141 查看
http://blog.csdn.net/blade2001/article/details/1542392
STL 线程安全性
SGI STL [http://www.sgi.com/tech/stl/thread_safety.html]
The SGI implementation of STL is thread-safe only in the sense that simultaneous accesses to distinct containers are safe, and simultaneous read accesses to to shared containers are safe. If multiple threads access a single container, and at least one thread
may potentially write, then the user is responsible for ensuring mutual exclusion between the threads during the container accesses.
http://www.csdn.net/Develop/read_article.asp?id=13936
摘录:
在所有的主流STL实现方案中,几乎所有的容器都是线程安全的:
1).一个线程读写一个实例时,另一个线程可以读写另一个实例。
2).多个线程可以在同时读同一个container。
3).多个线程读写同一个container时,你应该负责安排互斥性操作。
一个特例是std::string。在一些STL的实现厂商(包括MS VC6),使用的是带引用计数的string! 这就意味着可能有两个std::string的实例共享着同一块底层数据。这使得前面说的第一个规则被打破!
看一下这样的代码:
string s1= “abcd”;
string s2 = s1;
在引用计数的实现版本中,这几句话意味着:先分配一块内存给”abcd”,一个引用计数的数;s1和s2都将引用这块内存,引用计数将为2。引用计数的本意是在把strings传出functions时优化copy行为。
但是这种算法并不是线程安全的!
如果你将s2传给另一个线程,那么就很可能有两个线程企图修改这同一块内存!那将会有不可预料的行为发生。
理论上,你可以在两个线程之间增加线程同步,但是这个同步的代价将会大于你从引用计数中获得的好处!
这就是为什么主流的STL厂商不再使用引用计数的原因。比如,Dinkumware STL shipped with VC7。
Visual C++的STL并不是thread-safe,在它的allocator中,没有针对thread的同步问题做过任何特殊的设计,如果你用multi-thread操作同一个list container的话,最好自己完成同步方面问题;但如果你使用的是STLport提供的STL或者SGI STL的话,就应该是thread-safe,它的allocator做过一些特殊的设计
STL 线程安全性
SGI STL [http://www.sgi.com/tech/stl/thread_safety.html]
The SGI implementation of STL is thread-safe only in the sense that simultaneous accesses to distinct containers are safe, and simultaneous read accesses to to shared containers are safe. If multiple threads access a single container, and at least one thread
may potentially write, then the user is responsible for ensuring mutual exclusion between the threads during the container accesses.
http://www.csdn.net/Develop/read_article.asp?id=13936
摘录:
在所有的主流STL实现方案中,几乎所有的容器都是线程安全的:
1).一个线程读写一个实例时,另一个线程可以读写另一个实例。
2).多个线程可以在同时读同一个container。
3).多个线程读写同一个container时,你应该负责安排互斥性操作。
一个特例是std::string。在一些STL的实现厂商(包括MS VC6),使用的是带引用计数的string! 这就意味着可能有两个std::string的实例共享着同一块底层数据。这使得前面说的第一个规则被打破!
看一下这样的代码:
string s1= “abcd”;
string s2 = s1;
在引用计数的实现版本中,这几句话意味着:先分配一块内存给”abcd”,一个引用计数的数;s1和s2都将引用这块内存,引用计数将为2。引用计数的本意是在把strings传出functions时优化copy行为。
但是这种算法并不是线程安全的!
如果你将s2传给另一个线程,那么就很可能有两个线程企图修改这同一块内存!那将会有不可预料的行为发生。
理论上,你可以在两个线程之间增加线程同步,但是这个同步的代价将会大于你从引用计数中获得的好处!
这就是为什么主流的STL厂商不再使用引用计数的原因。比如,Dinkumware STL shipped with VC7。
Visual C++的STL并不是thread-safe,在它的allocator中,没有针对thread的同步问题做过任何特殊的设计,如果你用multi-thread操作同一个list container的话,最好自己完成同步方面问题;但如果你使用的是STLport提供的STL或者SGI STL的话,就应该是thread-safe,它的allocator做过一些特殊的设计
相关文章推荐
- STL容器的线程安全性
- 对STL容器线程安全性的期待现实一些
- STL 线程安全性
- STL的线程安全性
- 对STL容器线程安全性的期待现实一些
- 条款12:对STL容器线程安全性的期待现实一些
- 对STL容器线程安全性的期待现实一些
- 对STL容器线程安全性的期待现实一些
- STL组件容器
- android ndk增加对stl的支持
- <Effective STL>笔记--容器
- HDU 1716 排列2 (STL全排)
- STL remove
- 【转载】C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
- stl之map erase方法的正确使用
- STL源码解析-01stl配置文件
- STL中关于set容器的总结
- STL-A ACbo a464 y needs your help again! (栈和队列)
- STL向量构造函数
- 标准模板库-STL