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>支持++和--的原子操作。

(其他可参考 https://blog.csdn.net/fengbingchun/article/details/73436710)

C++11之 std::atomic (不用锁实现线程互斥)的更多相关文章

  1. C++11 并发指南六(atomic 类型详解三 std::atomic (续))

    C++11 并发指南六( <atomic> 类型详解二 std::atomic ) 介绍了基本的原子类型 std::atomic 的用法,本节我会给大家介绍C++11 标准库中的 std: ...

  2. C++11 并发指南六( <atomic> 类型详解二 std::atomic )

    C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)  一文介绍了 C++11 中最简单的原子类型 std::atomic_flag,但是 std::atomic_flag ...

  3. 第31课 std::atomic原子变量

    一. std::atomic_flag和std::atomic (一)std::atomic_flag 1. std::atomic_flag是一个bool类型的原子变量,它有两个状态set和clea ...

  4. std::atomic和std::mutex区别

    ​ ​std::atomic介绍​ ​模板类std::atomic是C++11提供的原子操作类型,头文件 #include<atomic>.​在多线程调用下,利用std::atomic可实 ...

  5. 用C++11的std::async代替线程的创建

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...

  6. C++11多线程std::thread的简单使用

    在cocos2dx 2.0时代,我们使用的是pthread库,是一套用户级线程库,被广泛地使用在跨平台应用上.但在cocos2dx 3.0中并未发现有pthread的支持文件,原来c++11中已经拥有 ...

  7. C++11 使用 std::async创建异步程序

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...

  8. (原创)用C++11的std::async代替线程的创建

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + ); t.join(); 但是线程毕竟是属于比较 ...

  9. Cocos2dx 3.0 过渡篇(二十七)C++11多线程std::thread的简单使用(下)

    本篇接上篇继续讲:上篇传送门:http://blog.csdn.net/star530/article/details/24186783 简单的东西我都说的几乎相同了,想挖点深的差点把自己给填进去. ...

随机推荐

  1. 面试题中关于String的常见操作

    题目1: 将用户输入的一段话,每个单词的首字母大写, 每个单词之间的空格调整为只有一个,遇到数字,将数字与后一个单词用下划线 "_" 进行连接 题目2:将 i @@ am @@@ ...

  2. redhat 配置yum源(配置163 yum repo)

    一般安装好redhat后,不能注册的话,不能使用系统自带的yum源.但是我们可以自己配置yum源来解决这一问题.下面介绍下redhat配置163yum源. 1) 查看版本号和系统类别: cat /et ...

  3. npm i和npm install的区别

    最近人用npm i来直接安装模块,但是有会报错,用npm install就不会报错,刚开始百思不得其解,它俩明明是同一个东西 后来查npm的帮助指令发现还是没区别,npm i仅仅是npm instal ...

  4. 学习笔记56—Endnote参考文献格式调整

    论文写作

  5. eclipse中怎么调出左边项目列表,解决方法:主界面的最上面一栏的Window--ShowView--Project Explorer

    主界面的最上面一栏的Window--ShowView--Project Explorer

  6. 第 3 章 镜像 - 021 - Docker 镜像小结

    镜像小结 镜像的常用操作子命令: images    显示镜像列表 history   显示镜像构建历史 commit    从容器创建新镜像 build     从 Dockerfile 构建镜像 ...

  7. 雷林鹏分享:jQuery EasyUI 数据网格 - 添加分页组件

    jQuery EasyUI 数据网格 - 添加分页组件 本实例演示如何从服务器端加载数据,如何添加分页组件(pagination)到数据网格(datagrid). 创建数据网格(DataGrid) 为 ...

  8. 单细胞数据高级分析之构建成熟路径 | Identifying a maturation trajectory

    其实就是另一种形式的打分. 个人点评这种方法: 这篇文章发表在nature上,有点奇怪,个人感觉创新性和重要性还不够格,工具很多,但是本文基本都是自己开发的算法(毕竟satji就是搞统计出身的). 但 ...

  9. ERROR org.redisson.client.handler.CommandDecoder - Unable to decode data. channel

    一.异常出现的场景 某一天下午,测试突然跑过来说,IOS系统APP访问500,Android没问题.我的第一反应是那就奇怪了,调的接口都是一样的,莫非和系统有关系.而且这个错误重启服务后,过一段时间才 ...

  10. sgu 154

    Factorial 题意:能否找到一个数,它的阶乘后面0的个数为n? 数越大,阶乘后的0越多.用二分找.对于一个数x,它的阶乘,将小于等于它的数分解质因数.其中2的个数一定大于5的个数.因此计5的个数 ...