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.

Effective STL 学习笔记: Thread Safety and STL Container的更多相关文章

  1. 【STL学习笔记】一、STL体系

    目录 1.标准库以header files形式呈现 2.namespce命名空间 3.STL与OO 4.STL六组件及其关系 5.STL组件例子 6.range-based for statement ...

  2. STL学习笔记— —无序容器(Unordered Container)

    简单介绍 在头文件<unordered_set>和<unordered_map> 中定义 namespace std { template <typename T, ty ...

  3. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  4. 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 ...

  5. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  6. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  7. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  8. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

  9. 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 ...

  10. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

随机推荐

  1. web中的懒加载

    在Web应用程序中,系统的瓶颈常在于系统的响应速度.如果系统响应速度过慢,用户就会出现埋怨情绪,系统的价值也因此会大打折扣.因此,提高系统响应速度,是非常重要的. Web应用程序做的最多就是和后台数据 ...

  2. Laravel 项目集合

    1.  CMS LaraCMS  https://github.com/wanglelecc/laracms 2. 电商 3.  点播 MeEdu      https://github.com/Qs ...

  3. Ubuntu 18.04设置dns

    最近使用了最新版的ubuntu 18.04运行一些服务,然后发现服务器经常出现网络不通的情况,主要是一些域名无法解析. 检查/etc/resolv.conf,发现之前修改的nameserver总是会被 ...

  4. PHP5下WSDL,SOAP调用实现过程

    一.基础概念 SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协议,它包括四个部分:SOAP封装 ...

  5. bzoj千题计划125:bzoj1037: [ZJOI2008]生日聚会Party

    http://www.lydsy.com/JudgeOnline/problem.php?id=1037 一个区间是否满足 任意连续的一段,男孩与女孩的数目之差不超过k, 取决于男孩与女孩数目之差的最 ...

  6. HDU 3032 multi-sg 打表找规律

    普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...

  7. Matlab debug

    输入彩色,imwrite保存黑白图片,imwrite的维度错误. 程序如下,正常图像,少了一个维度imwrite,把图片展开,是一个二维的灰色图像(R=G=B),.如果限定了第二维,也是一个灰色图像. ...

  8. python学习笔记2--list

    一.list操作 list=['xiaohei','xiaobai','xiaohong']#定义一个数组 name=[]#定义一个空数组 #查 #下标是从0开始 print(list[1]) #-1 ...

  9. don't run elasticsearch as root.

    因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户 第一步:liunx创建新用户  adduser XXX    然后给创建的用户加密码 passwd XXX    ...

  10. Postgresql数据库安装中文全文搜索插件zhparser的问题

    在PG数据库的基础上加装zhparser中文全文搜索插件,说实话,挺怕这些单独编译安装的插件的,因为安装PG数据库方法的不同,最后可能导致安装的插件各种安装不上,这里说一下我遇到的坑,系统环境是Cen ...