c++11新特性之atomic
- 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.
- #include <iostream>
- #include <atomic>
- #include <vector>
- #include <thread>
- #include <sstream>
- std::atomic_flag lock = ATOMIC_FLAG_INIT;
- std::stringstream stream;
- void append_numer(int x)
- {
- while (lock.test_and_set());
- stream << "thread#" << x << "\n";
- lock.clear();
- }
- int main()
- {
- std::vector<std::thread> ths;
- for (int i=; i<; i++)
- ths.push_back(std::thread(append_numer, i));
- for (int i=; i<; i++)
- ths[i].join();
- std::cout << stream.str();
- return ;
- }
2. std::atomic
std::atomic对int, char, bool等数据结构进行原子性封装,在多线程环境中,对std::atomic对象的访问不会造成竞争-冒险。利用std::atomic可实现数据结构的无锁设计。
- #include <iostream>
- #include <atomic>
- #include <vector>
- #include <thread>
- #include <sstream>
- std::atomic<bool> ready(false);
- std::atomic_flag winner = ATOMIC_FLAG_INIT;
- void count1m(int i)
- {
- while (!ready);
- for (int i=; i<; i++);
- if (!winner.test_and_set())
- std::cout << "winner: " << i << std::endl;
- }
- int main()
- {
- std::vector<std::thread> ths;
- for (int i=; i<; i++)
- ths.push_back(std::thread(count1m, i));
- ready = true;
- for (int i=; i<; i++)
- ths[i].join();
- return ;
- }
在上例中,执行read=true之后,所有线程结束空等。winner被初始化为ATOMIC_FLAG_INIT,最先执行winner.test_and_set并返回false的线程为winner。
- #include <iostream>
- #include <atomic>
- #include <vector>
- #include <thread>
- #include <sstream>
- std::atomic<int> foo();
- void set_foo(int x)
- {
- foo = x;
- }
- void print_foo()
- {
- while (foo == )
- {
- std::this_thread::yield();
- }
- std::cout << "x: " << foo << std::endl;
- }
- int main()
- {
- std::thread print_th(print_foo);
- std::thread set_th(set_foo, );
- print_th.join();
- set_th.join();
- return ;
- }
在上例总,set_foo用于设置atomic<int>对象的值,print_foo用于打印atomic<int>对象的值。std::atomic对象的值的读取和写入可使用load和store实现。
- #include <iostream>
- #include <cassert>
- #include <atomic>
- #include <vector>
- #include <unistd.h>
- #include <thread>
- #include <sstream>
- std::atomic<int> foo();
- std::atomic_flag lock = ATOMIC_FLAG_INIT;
- void add_foo()
- {
- while ()
- {
- foo++;
- // foo = foo + 1;
- while (lock.test_and_set());
- std::cout <<"add: " << foo << std::endl;
- lock.clear();
- usleep();
- }
- }
- void sub_foo()
- {
- while ()
- {
- foo--;
- // foo = foo - 1;
- while (lock.test_and_set());
- std::cout << "sub: " << foo << std::endl;
- lock.clear();
- usleep();
- }
- }
- int main()
- {
- std::thread th2 = std::thread(add_foo);
- std::thread th1 = std::thread(sub_foo);
- th1.join();
- th2.join();
- return ;
- }
atomic<int>支持++和--的原子操作。
c++11新特性之atomic的更多相关文章
- c++ 11 线程池---完全使用c++ 11新特性
前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...
- C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)
因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...
- C++11新特性总结 (二)
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
- C++11新特性总结 (一)
1. 概述 最近在看C++ Primer5 刚好看到一半,总结一下C++11里面确实加了很多新东西,如果没有任何了解,别说自己写了,看别人写的代码估计都会有些吃力.C++ Primer5是学习C++1 ...
- C++ 11 新特性
C++11新特性: 1.auto 2.nullptr 3.for 4.lambda表达式 5.override ...
- [转载] C++11新特性
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
- 在C++98基础上学习C++11新特性
自己一直用的是C++98规范来编程,对于C++11只闻其名却没用过其特性.近期因为工作的需要,需要掌握C++11的一些特性,所以查阅了一些C++11资料.因为自己有C++98的基础,所以从C++98过 ...
- C++11新特性——range for
很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中.range for语句,可以方便的遍历给定序列中的每个元素并对其执行某种操作. 1.基本语法 for(d ...
- C++11新特性——大括号初始化
C++11之前,C++主要有以下几种初始化方式: //小括号初始化 string str("hello"); //等号初始化 string str="hello" ...
随机推荐
- JavaScript 数组操作方法 和 ES5数组拓展
JavaScript中数组有各种操作方法,以下通过举例来说明各种方法的使用: 数组操作方法 push 在数组最后添加一个元素 var arr=[3,4,5,6] console.log(arr) // ...
- Python replace方法并不改变原字符串
直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...
- 阿里校招内推C++岗位编程题第一题 空格最少的字符串
给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成.并输出解. 如果没有解则应该输出n/a 例如: 输入: S = “ilikealibab ...
- 2019寒假训练营寒假作业(三) MOOC的网络空间安全概论笔记部分
目录 第五章 网络攻防技术 5.1:网络信息收集技术--网络踩点 信息收集的必要性及内容 网络信息收集技术 网络踩点(Footprinting) 网络踩点常用手段 5.2:网络信息收集技术 --网络扫 ...
- [基于NetCore的简单博客系统]-登录
0-项目背景 一个基于.NET CORE RAZOR PAGES的简单博客系统 技术栈全部采用微软官方实现方式,目的是熟悉新技术 项目地址:https://github.com/ganqiyin/BL ...
- j2ee—框架(1):Servlet+JSP实现基本的登录功能(v1.0)
主要分为四个部分:LoginController.web.xml.login.jsp和login_success.jsp(login_fail.jsp). 第一部分 LoginController p ...
- (五)final修饰符
final修饰变量 final修饰符一般用于基本数据类型(int,float)或者不可变对象(String).这时候可以看作常量变量. 但是当final作用于可变数据类型时(数组,一般对象),仅仅表示 ...
- iOS-AFNetworking与ASIHTTPRequest的区别
一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession 2.ASI的底层实现基于纯C语言的CFNetwork框架 3.因为NSURLConnectio ...
- TCP系列19—重传—9、thin stream下的重传
一.介绍 当TCP连续大量的发送数据的时候,当出现丢包的时候可以有足够的dup ACK来触发快速重传.但是internet上还有大量的交互式服务,这类服务一般都是由小包组成,而且一次操作中需要传输的数 ...
- android gradle打包常见问题及解决方案
背景: 问题: Q1: UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: Java heap space at com.android.d ...