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. struts的namespace理解

    转载: namespace决定了action的访问路径,默认为"",可以接受所有路径的action namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的acti ...

  2. 【Asp.net入门5-03】创建产品清单

  3. range循环

    for i in range(10): #特殊写法,从0开始,步长为1,最大值小于10 print("loop",i) print("=========") f ...

  4. 《剑指offer》面试题45 圆圈中最后剩下的数字(Java版本)

    引言 这道题网上的讲解都是直接抄书,没意思,所以想自己写一写,补充一下,便于自己理解.另外,大家都忽略了经典解法,虽然这种解法效率不及第二种,但是我觉得在项目当中阅读性其实很重要,牺牲一点点效率保证代 ...

  5. html5 +css3 点击后水波纹扩散效果 兼容移动端

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. SQL语句(二十)—— 数据库安全性

    数据库安全性 1. SQL Server 配置管理器  => 网络配置 MSSQLSERVER 协议,如果应用程序和SQL Server 在同一机器上,仅使用 Shared Memory (共享 ...

  7. Java并发编程原理与实战四十:JDK8新增LongAdder详解

    传统的原子锁AtomicLong/AtomicInt虽然也可以处理大量并发情况下的计数器,但是由于使用了自旋等待,当存在大量竞争时,会存在大量自旋等待,而导致CPU浪费,而有效计算很少,降低了计算效率 ...

  8. HTML面试基础问题

    1.Doctype作用?严格模式与混杂模式如何区分?它们有何意义?   1)<!DICTYPE>声明位于文档中的最前面,处于<html>标签之前,告诉浏览器的解析器,用什么文档 ...

  9. LintCode 412: Candy

    LintCode 412: Candy 题目描述 有 N 个小孩站成一列.每个小孩有一个评级. 按照以下要求,给小孩分糖果: 每个小孩至少得到一颗糖果. 评级越高的小孩可以得到更多的糖果. 需最少准备 ...

  10. 20155315 2016-2017-2 《Java程序设计》第五周学习总结

    教材学习内容总结 第8章 异常处理 1.使用try...catch 与C语言中程序流程和错误处理混在一起不同,Java中把正常流程放try块中,错误(异常)处理放catch块中. 如果父类异常对象在子 ...