Effective STL 学习笔记: Thread Safety and STL Container
Table of Contents
1 STL, Thread and SGI
C++98 以及之前的 C++ 标准中,并未对线程做出过规定,但对于 STL 来讲,SGI 做出了自己的规定,很多其他的 STL 实现也遵循这些规定:
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.
This is the only way to ensure full performance for containers that do not need concurrent access. Locking or other forms of synchronization are typically expensive and should be avoided when not necessary.
It is easy for the client or another library to provide the necessary locking by wrapping the underlying container operations with a lock acquisition and release. For example, it would be possible to provide a locked_queue container adapter that provided a container with atomic queue operations.
For most clients, it would be insufficient to simply make container operations atomic; larger grain atomic actions are needed. If a user's code needs to increment the third element in a vector of counters, it would be insuffcient to guarantee that fetching the third element and storing the third element is atomic; it is also necessary to guarantee that no other updates occur in the middle. Thus it would be useless for vector operations to acquire the lock; the user code must provide for locking in any case.
This decision is different from that made by the Java designers. There are two reasons for that. First, for security reasons Java must guarantee that even in the presence of unprotected concurrent accesses to a container, the integrity of the virtual machine cannot be violated. Such safety constraints were clearly not a driving force behind either C++ or STL. Secondly, performance was a more important design goal for STL than it was for the Java standard library.
On the other hand, this notion of thread-safety is stronger than that provided by reference-counted string implementations that try to follow the CD2 version of the draft standard. Such implementations require locking between multiple readers of a shared string.
2 STL and Lock
2.1 RAII
需要对一个容器进行写操作的时候,我们可以加锁保护:
1: // skeletal template for classes that acquire and release mutexes for containers; many
2: // details have been omitted
3: template<typename Container> class Lock
4: {
5: public:
6: Lock(const Containers container) : c(container)
7: {
8: getMutexFor(c); // Acquire mutex in constructor
9: }
10: ~Lock()
11: {
12: releaseMutexFor(c); // Release mutex in constructor
13: }
14: private:
15: const Container& c;
16: };
这里用到了一个技巧 Resource Acquisition Is Initialization ( RAIII ) ,其基本思想包括:
- 所用资源在对象构造时就初始化好
这样可以保证用到该对象时,所需访问的资源始终为 Valid 状态。 - 所用资源在对象析构时释放
这样就保证了资源可以自动释放。
2.2 Use Lock in STL
17: vector<int>v;
18: {
19: Lock<vector<int> > lock(v); // Lock initialized here.
20: vector<int>::iterator frist5(find(v.begin(), v.end(), 5));
21: if (first5 != v.end())
22: {
23: *first5 = 0;
24: }
25: } // Lock will be released after exit this scope.
(使用许可:署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议 。)
Effective STL 学习笔记: Thread Safety and STL Container的更多相关文章
- 【STL学习笔记】一、STL体系
目录 1.标准库以header files形式呈现 2.namespce命名空间 3.STL与OO 4.STL六组件及其关系 5.STL组件例子 6.range-based for statement ...
- STL学习笔记— —无序容器(Unordered Container)
简单介绍 在头文件<unordered_set>和<unordered_map> 中定义 namespace std { template <typename T, ty ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor
Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
随机推荐
- eclipse安装activiti5.18.0工作流插件 以及安装过程中activiti插件出现的问题及解决
转: eclipse安装activiti5.18.0工作流插件 以及安装过程中activiti插件出现的问题及解决 2017年05月04日 18:44:21 JJ_nan 阅读数:2773 版权声 ...
- Docker 安装tensorflow
安装DOCKER 1. https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/ nstall from a packag ...
- Python 爬虫入门(二)—— IP代理使用
上一节,大概讲述了Python 爬虫的编写流程, 从这节开始主要解决如何突破在爬取的过程中限制.比如,IP.JS.验证码等.这节主要讲利用IP代理突破. 1.关于代理 简单的说,代理就是换个身份.网络 ...
- 【Asp.net入门05】第一个ASP.NET 应用程序-测试Asp.net程序
测试示例应用程序 本部分内容: ASP.NET应用程序测试方法 web窗体访问过程 Visual Studio工具栏上有一个下拉列表,其中列出了工作站上已安装的浏览器的名称(单击浏览器名称右侧的向下箭 ...
- 星号三角形 I
N = int(eval(input())) for row in range(1,N+1): if row%2 != 0: a = '*'*row print ('{}'.format(a.cent ...
- 转:CocoaPods pod install/pod update更新慢的问题
最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...
- OC中线程安全的单例
@implementation MySingleton + (instancetype)sharedInstance { static MySingleton* instance = nil; sta ...
- 科学计算三维可视化---Traits介绍
简介 Traits是开源扩展库,Traits本身与科学计算可视化没有直接关联,但他其实TVTK,Mayavi,TraitsUI基础 安装: pip3 install traits--cp36-cp36 ...
- SVN启停脚本
说明:特别注意红色部分,外部$1传入Msg函数时失效,故特此读取一遍再传入!执行时要给脚本加执行权限!#chmod 755 /scripts/svn [root@kazihuo /scripts]# ...
- Spyder简述
导言 想打造轮子, 就必须要有一套完善的造轮子的工具. 我在jupyter+sciTE的组合里转来转去, 最后还是打算放弃这个组合, 因为离开了自动完成/调用提示/随时随地的访问文档帮助, 前行之路太 ...