博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++11线程指南(6)--共享内存与互斥
阅读量:4070 次
发布时间:2019-05-25

本文共 2574 字,大约阅读时间需要 8 分钟。

目录


1. 共享内存

  先回顾一下前面几章中用到的,一个存在资源竞争的例子:

#include
#include
void ThreadFunc() { for (int i = 1; i <= 8; ++i) std::cout << "thread function: " << i << "\n";}int main(){ std::thread thr(&ThreadFunc); for (int i = 1; i <= 8; ++i) std::cout << "main thread: " << i << "\n"; thr.join(); return 0;}

  运行结果为:

main thread: 1
main thread: 2
main thread: 3
main thread: 4
main thread: 5
main thread: 6
main thread: 7
thread function: main thread: 8
1
thread function: 2
thread function: 3
thread function: 4
thread function: 5
thread function: 6
thread function: 7
thread function: 8

  可以看到,结果是无序的。

2.使用互斥量mutex

下面使用互斥对线程间进行同步。

#include
#include
#include
#include
std::mutex mu;void PrintFunc(std::string msg, int id) { mu.lock(); std::cout<
<<":"<
<

  运行结果为:

main thread:1
thread function:1
thread function:2
thread function:3
main thread:2
main thread:3
main thread:4
thread function:4
thread function:5
thread function:6
main thread:5
main thread:6
main thread:7
main thread:8
thread function:7
thread function:8

3.共享数据问题

  多线程之间共享数据的问题,很大程度上源自对数据的修改顺序。

  如果共享数据是只读的,则不会存在问题,因为一个线程读数据不会影响到另一个线程读取相同数据。但是,但一个线程或多个线程可以修改数据时,问题就出现了。
  下面介绍的互斥就是用于解决这种问题的。

4.互斥量mutex释放

  下面例子中多个线程同时访问相同的list

#include
#include
#include
#include
std::list
myList;void AddToList(int max, int interval) { for(int i=0;i

  运行结果为:

  0,2,4,6,8,10,12,14,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
  或者
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,2,4,6,8,10,12,14,
  在对list的操作过程中,t1会插入0到9的整数,而t2会插入每个2的倍数。同时,t3会打印list元素。上面的运行结果显示,打印完全处于无序状态。
  互斥可以说是C++的数据保护中使用最广泛的技术。它可以保护数据,避免资源竞争,但同时也会带来死锁,或者保护了太多的或太少的数据(互斥范围选择),等问题。
  C++中可以使用std::mutex()创建实例,调用lock()来锁定资源,unlock()释放资源。但是,可能存在unLock()没有被调用进行释放。
  例如,lock()之后并且unlock()之前,抛出了一个异常。这样导致资源一直处于被锁状态。所以,在标准C++库中,提供了std::lock_guard类模板。它在构造时会锁定指定的mutex, 析构时解锁,确保一个锁定的资源最后都能解锁。

#include
#include
#include
#include
#include
std::list
myList;std::mutex myMutex;void AddToList(int max, int interval) { std::lock_guard
guard(myMutex); for(int i=0;i
guard(myMutex); for(auto &iter: myList) std::cout<
<<",";}int main() { int max = 15; std::thread t1(AddToList, max, 1); std::thread t2(AddToList, max, 2); std::thread t3(PrintList); t1.join(); t2.join(); t3.join(); return 0;}

  运行的其中一种结果为:

  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0,2,4,6,8,10,12,14,
  从上看到,2的整数倍的数是连续的。加了互斥机制后,保证了多线程的顺序性。

转载地址:http://fmeji.baihongyu.com/

你可能感兴趣的文章
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
WAV文件解析
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
QT打开项目提示no valid settings file could be found
查看>>
android 代码实现圆角
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
Visual Studio 2010:C++0x新特性
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
Encoding Schemes
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Java8 HashMap集合解析
查看>>