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.

#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的更多相关文章

  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. Spring 3整合Quartz 2实现定时任务:动态添加任务

    先展示一下后台管理定时任务效果图: 1.新增任务页面: 2.列表页(实现任务的禁用启用) 3.数据库脚本: -- ------------------------------ Table struct ...

  2. Too many open files错误与解决方法

    致前辈:该问题的解决思路给了我很大的启发,文章作者Lis, Linux资深技术专家. 问题现象:这是一个基于Java的web应用系统,在后台添加数据时提示无法添加,于是登陆服务器查看Tomcat 日志 ...

  3. 安装HIVE

    参考:https://cwiki.apache.org/confluence/display/Hive/GettingStarted 1.下载hive安装包     到apache官网或者其它地方下载 ...

  4. angularJS遇到的坑

    最近在用angularjs做一些东西,由于学艺不精,对angularjs了解不够,导致经常会不小心掉进一些自己挖的坑里(⊙_⊙),在这里记下来,谨防又踩. 1.angularjs ng-show no ...

  5. 拓扑排序(Toposort)

    摘自:https://blog.csdn.net/qq_35644234/article/details/60578189 <图论算法> 1.拓扑排序的介绍 对一个有向无环图(Direct ...

  6. Thunder团队第三周 - Scrum会议5

    Scrum会议5 小组名称:Thunder 项目名称:i阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  7. Android开发 使用 adb logcat 显示 Android 日志

    作者 : 万境绝尘  转载请著名出处 eclipse 自带的 LogCat 工具太垃圾了, 开始用 adb logcat 在终端查看日志; 1. 解析 adb logcat 的帮助信息 在命令行中输入 ...

  8. linux线程同步实例

    [Linux多线程]三个经典同步问题 - 神奕的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/lisonglisonglisong/article/details ...

  9. Vim新手节省时间的10多个小技巧

    Vim新手节省时间的10多个小技巧 Vim 是很多开发者的首选编辑器,通过设置正确的命令和快捷方式,它可以帮你更快的完成工作.这篇文章我们为 Vim 新手提供一些快捷键等方面的小技巧,帮你提升工作效率 ...

  10. dede5.7文章模型(非软件模型)添加下载附件的方法

    添加字段 ---- > 字段类型为 附件 --- - > templets/system/channel_addon.htm 代码清空,只保留 ~link~ -+---> 保存. & ...