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. 1: // skeletal template for classes that acquire and release mutexes for containers; many
  2. 2: // details have been omitted
  3. 3: template<typename Container> class Lock
  4. 4: {
  5. 5: public:
  6. 6: Lock(const Containers container) : c(container)
  7. 7: {
  8. 8: getMutexFor(c); // Acquire mutex in constructor
  9. 9: }
  10. 10: ~Lock()
  11. 11: {
  12. 12: releaseMutexFor(c); // Release mutex in constructor
  13. 13: }
  14. 14: private:
  15. 15: const Container& c;
  16. 16: };

这里用到了一个技巧 Resource Acquisition Is Initialization ( RAIII ) ,其基本思想包括:

  • 所用资源在对象构造时就初始化好
    这样可以保证用到该对象时,所需访问的资源始终为 Valid 状态。
  • 所用资源在对象析构时释放
    这样就保证了资源可以自动释放。

2.2 Use Lock in STL

  1. 17: vector<int>v;
  2. 18: {
  3. 19: Lock<vector<int> > lock(v); // Lock initialized here.
  4. 20: vector<int>::iterator frist5(find(v.begin(), v.end(), 5));
  5. 21: if (first5 != v.end())
  6. 22: {
  7. 23: *first5 = 0;
  8. 24: }
  9. 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. Java入门:一些初学者需要掌握的基础算法程序——二分查找

    本例演示如何通过二分算法查找一个链表中的指定元素. import java.util.Scanner; class BinarySearchExample { public static void m ...

  2. 在阿里云上无法使用mailx发送邮件的解决办法,验证可用。

    由于阿里云已将25端口封了(改用465端口),所以在ECS上往外发邮件时要作相应的配置才行. 使用的是163的企业邮箱,笔记简洁可用. 在阿里云的“安全组”开放645端口通行. 1.安装相应软件包: ...

  3. insert tp5.1

    $insertId = Db::name('user_address')->insertGetId($data); 会插入数据,返回插入的 id //$flag = Db::name('user ...

  4. 触发器的SQL语法

    create trigger triggerName after/before insert/update/delete on 表名 for each row #这句话在mysql是固定的 begin ...

  5. unity解析json的两种方式

    一直比较钟情于json,用来做数据交互,堪称完美!下面简单说一下unity使用C#脚本如何解析json数据吧. 一.写解析类,借助于JsonUtility.FromJson 直接给个例子吧 1.jso ...

  6. Java基础-IO流对象之打印流(PrintStream与PrintWriter)

    Java基础-IO流对象之打印流(PrintStream与PrintWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.打印流的特性 打印对象有两个,即字节打印流(P ...

  7. MetaBase

    MetaBase是一个快速创建图表的Web站点,对于频繁上新项目,频繁提供数据报表,但人力不足的情况下,是一个不错的选择. 一. 安装部署 在windows环境下可以使用jar.docker的方式,本 ...

  8. 关于构造IOCTL命令的学习心得

    在编写ioctl代码之前,需要选择对应不同命令的编号.为了防止对错误的设备使用正确的命令,命令号应该在系统范围内唯一,这种错误匹配并不是不会发生,程序可能发现自己正在试图对FIFO和audio等这类非 ...

  9. warshall-floyd算法:POJ No 2139 Six Degress of Cowvin Bacon(任意两点最短路径))

    题目: http://poj.org/problem?id=2139 题解:N只牛,在一组的两只牛,分别两只之间为 “1度”,自己到自己为0度,M组牛.求,N只牛之中,两只牛之间 平均最短度数*100 ...

  10. HBase基本概念

    HBase是什么 HBase构建在 HDFS 之上的分布式列式键值存储系统.HBase内部管理的文件全部存储在HDFS中. HBase VS HDFS HDFS适合批处理场景 不支持数据随机查找 不适 ...