1. std::atomic_flag

  std::atomic_flag是一个原子的布尔类型,可支持两种原子操作:

  • test_and_set, 如果atomic_flag对象被设置,则返回true; 如果atomic_flag对象未被设置,则设置之,返回false
  • clear. 清楚atomic_flag对象

  std::atomic_flag可用于多线程之间的同步操作,类似于linux中的信号量。使用atomic_flag可实现mutex.

  1. #include <iostream>
  2. #include <atomic>
  3. #include <vector>
  4. #include <thread>
  5. #include <sstream>
  6.  
  7. std::atomic_flag lock = ATOMIC_FLAG_INIT;
  8. std::stringstream stream;
  9.  
  10. void append_numer(int x)
  11. {
  12. while (lock.test_and_set());
  13. stream << "thread#" << x << "\n";
  14. lock.clear();
  15. }
  16.  
  17. int main()
  18. {
  19. std::vector<std::thread> ths;
  20. for (int i=; i<; i++)
  21. ths.push_back(std::thread(append_numer, i));
  22. for (int i=; i<; i++)
  23. ths[i].join();
  24. std::cout << stream.str();
  25. return ;
  26. }

  2. std::atomic

  std::atomic对int, char, bool等数据结构进行原子性封装,在多线程环境中,对std::atomic对象的访问不会造成竞争-冒险。利用std::atomic可实现数据结构的无锁设计。

  1. #include <iostream>
  2. #include <atomic>
  3. #include <vector>
  4. #include <thread>
  5. #include <sstream>
  6.  
  7. std::atomic<bool> ready(false);
  8. std::atomic_flag winner = ATOMIC_FLAG_INIT;
  9.  
  10. void count1m(int i)
  11. {
  12. while (!ready);
  13. for (int i=; i<; i++);
  14. if (!winner.test_and_set())
  15. std::cout << "winner: " << i << std::endl;
  16. }
  17.  
  18. int main()
  19. {
  20. std::vector<std::thread> ths;
  21. for (int i=; i<; i++)
  22. ths.push_back(std::thread(count1m, i));
  23. ready = true;
  24. for (int i=; i<; i++)
  25. ths[i].join();
  26. return ;
  27. }

  在上例中,执行read=true之后,所有线程结束空等。winner被初始化为ATOMIC_FLAG_INIT,最先执行winner.test_and_set并返回false的线程为winner。

  1. #include <iostream>
  2. #include <atomic>
  3. #include <vector>
  4. #include <thread>
  5. #include <sstream>
  6.  
  7. std::atomic<int> foo();
  8.  
  9. void set_foo(int x)
  10. {
  11. foo = x;
  12. }
  13.  
  14. void print_foo()
  15. {
  16. while (foo == )
  17. {
  18. std::this_thread::yield();
  19. }
  20. std::cout << "x: " << foo << std::endl;
  21. }
  22. int main()
  23. {
  24. std::thread print_th(print_foo);
  25. std::thread set_th(set_foo, );
  26. print_th.join();
  27. set_th.join();
  28. return ;
  29. }

  在上例总,set_foo用于设置atomic<int>对象的值,print_foo用于打印atomic<int>对象的值。std::atomic对象的值的读取和写入可使用load和store实现。

  

  1. #include <iostream>
  2. #include <cassert>
  3. #include <atomic>
  4. #include <vector>
  5. #include <unistd.h>
  6. #include <thread>
  7. #include <sstream>
  8.  
  9. std::atomic<int> foo();
  10. std::atomic_flag lock = ATOMIC_FLAG_INIT;
  11.  
  12. void add_foo()
  13. {
  14. while ()
  15. {
  16. foo++;
  17. // foo = foo + 1;
  18. while (lock.test_and_set());
  19. std::cout <<"add: " << foo << std::endl;
  20. lock.clear();
  21. usleep();
  22. }
  23. }
  24.  
  25. void sub_foo()
  26. {
  27. while ()
  28. {
  29. foo--;
  30. // foo = foo - 1;
  31. while (lock.test_and_set());
  32. std::cout << "sub: " << foo << std::endl;
  33. lock.clear();
  34. usleep();
  35. }
  36. }
  37. int main()
  38. {
  39. std::thread th2 = std::thread(add_foo);
  40. std::thread th1 = std::thread(sub_foo);
  41. th1.join();
  42. th2.join();
  43. return ;
  44. }

  atomic<int>支持++和--的原子操作。

c++11新特性之atomic的更多相关文章

  1. c++ 11 线程池---完全使用c++ 11新特性

    前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...

  2. C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)

    因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...

  3. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

  4. C++11新特性总结 (一)

    1. 概述 最近在看C++ Primer5 刚好看到一半,总结一下C++11里面确实加了很多新东西,如果没有任何了解,别说自己写了,看别人写的代码估计都会有些吃力.C++ Primer5是学习C++1 ...

  5. C++ 11 新特性

    C++11新特性:          1.auto          2.nullptr          3.for          4.lambda表达式          5.override ...

  6. [转载] C++11新特性

    C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...

  7. 在C++98基础上学习C++11新特性

    自己一直用的是C++98规范来编程,对于C++11只闻其名却没用过其特性.近期因为工作的需要,需要掌握C++11的一些特性,所以查阅了一些C++11资料.因为自己有C++98的基础,所以从C++98过 ...

  8. C++11新特性——range for

    很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中.range for语句,可以方便的遍历给定序列中的每个元素并对其执行某种操作. 1.基本语法 for(d ...

  9. C++11新特性——大括号初始化

    C++11之前,C++主要有以下几种初始化方式: //小括号初始化 string str("hello"); //等号初始化 string str="hello" ...

随机推荐

  1. JavaScript 数组操作方法 和 ES5数组拓展

    JavaScript中数组有各种操作方法,以下通过举例来说明各种方法的使用: 数组操作方法 push 在数组最后添加一个元素 var arr=[3,4,5,6] console.log(arr) // ...

  2. Python replace方法并不改变原字符串

    直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...

  3. 阿里校招内推C++岗位编程题第一题 空格最少的字符串

    给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成.并输出解. 如果没有解则应该输出n/a 例如: 输入: S = “ilikealibab ...

  4. 2019寒假训练营寒假作业(三) MOOC的网络空间安全概论笔记部分

    目录 第五章 网络攻防技术 5.1:网络信息收集技术--网络踩点 信息收集的必要性及内容 网络信息收集技术 网络踩点(Footprinting) 网络踩点常用手段 5.2:网络信息收集技术 --网络扫 ...

  5. [基于NetCore的简单博客系统]-登录

    0-项目背景 一个基于.NET CORE RAZOR PAGES的简单博客系统 技术栈全部采用微软官方实现方式,目的是熟悉新技术 项目地址:https://github.com/ganqiyin/BL ...

  6. j2ee—框架(1):Servlet+JSP实现基本的登录功能(v1.0)

    主要分为四个部分:LoginController.web.xml.login.jsp和login_success.jsp(login_fail.jsp). 第一部分 LoginController p ...

  7. (五)final修饰符

    final修饰变量 final修饰符一般用于基本数据类型(int,float)或者不可变对象(String).这时候可以看作常量变量. 但是当final作用于可变数据类型时(数组,一般对象),仅仅表示 ...

  8. iOS-AFNetworking与ASIHTTPRequest的区别

    一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession  2.ASI的底层实现基于纯C语言的CFNetwork框架  3.因为NSURLConnectio ...

  9. TCP系列19—重传—9、thin stream下的重传

    一.介绍 当TCP连续大量的发送数据的时候,当出现丢包的时候可以有足够的dup ACK来触发快速重传.但是internet上还有大量的交互式服务,这类服务一般都是由小包组成,而且一次操作中需要传输的数 ...

  10. android gradle打包常见问题及解决方案

    背景: 问题: Q1: UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: Java heap space at com.android.d ...