原子操作atomic解读
下面从一个问题引入:
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<random>
#include<iostream>
#include<vector>
#include<thread>
#include<algorithm>
#include<future>
using namespace std;
int x = 0;
void mythread_one()
{
for (int i = 1; i < 1000000; i++)
{
x++;
}
} void mythread_two()
{
for (int i = 0; i < 1000000; i++)
{
x++;
}
}
int main()
{ thread th_one(mythread_one);
thread th_two(mythread_two);
th_one.join();
th_two.join(); cout << "x的值为:" << x << endl; return 0;
}
执行结果:

这段程序设置了两个线程,然后对全局变量进行加加操作,但是执行的结果却不是我们真正想要的。
解决办法可以对访问的数据加锁:
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<random>
#include<iostream>
#include<vector>
#include<thread>
#include<algorithm>
#include<future>
using namespace std;
int x = 0;
mutex g;
void mythread_one()
{
for (int i = 1; i < 10000000; i++)
{
g.lock();
x++;
g.unlock();
}
} void mythread_two()
{
for (int i = 0; i < 10000000; i++)
{
g.lock();
x++;
g.unlock();
}
}
int main()
{ thread th_one(mythread_one);
thread th_two(mythread_two);
th_one.join();
th_two.join(); cout << "x的值为:" << x << endl; return 0;
}
这样的确可以解决问题,但是为了提升效率,C++11引入了原子操作atomic<>,它能够保证在读取数据的时候不会被打断,和加锁机制相比,他的效率更高.
代码演示:
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<random>
#include<iostream>
#include<vector>
#include<thread>
#include<algorithm>
#include<future>
using namespace std;
int x = 0;
atomic<int> g = 0;
void mythread_one()
{
for (int i = 1; i < 10000000; i++)
{
g++;
}
} void mythread_two()
{
for (int i = 0; i < 10000000; i++)
{
g++;
}
}
int main()
{ thread th_one(mythread_one);
thread th_two(mythread_two);
th_one.join();
th_two.join(); cout << "x的值为:" << x << endl; return 0;
}
接下来再看另一个原子操作atomic<bool> ,他用于终止线程的执行:
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<random>
#include<iostream>
#include<vector>
#include<thread>
#include<algorithm>
#include<future>
using namespace std;
int x = 0;
atomic<bool> g_ifend=false;
void mythread()
{
cout << "子线程1开始执行了" << endl;
while (g_ifend == false)
{
cout << "任务执行中..." << endl;
this_thread::sleep_for(chrono::seconds(2));
}
cout << "子线程任务执行结束" << endl;
} int main()
{ cout << "主线程开始执行了" << endl;
thread th(mythread);
this_thread::sleep_for(chrono::seconds(5));
g_ifend = true;//让主线程等待5秒,过了5秒,让子线程结束任务
th.join();
cout << "主线程执行任务结束了" << endl; return 0;
}
原子操作atomic解读的更多相关文章
- 并发编程之原子操作Atomic&Unsafe
原子操作:不能被分割(中断)的一个或一系列操作叫原子操作. 原子操作Atomic主要有12个类,4种类型的原子更新方式,原子更新基本类型,原子更新数组,原子更新字段,原子更新引用.Atomic包中的类 ...
- java并发:线程同步机制之Volatile关键字&原子操作Atomic
volatile关键字 volatile是一个特殊的修饰符,只有成员变量才能使用它,与Synchronized及ReentrantLock等提供的互斥相比,Synchronized保证了Synchro ...
- 什么是Java中的原子操作( atomic operations)
1.啥是java的原子性 原子性:即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行. 一个很经典的例子就是银行账户转账问题: 比如从账户A向账户B转1000元,那么 ...
- golang中的原子操作atomic包
1. 概念 原子操作 atomic 包 加锁操作涉及到内核态的上下文切换,比较耗时,代价高, 针对基本数据类型我们还可以使用原子操作来保证并发的安全, 因为原子操作是go语言提供的方法,我们在用户态就 ...
- C++11并发编程:原子操作atomic
一:概述 项目中经常用遇到多线程操作共享数据问题,常用的处理方式是对共享数据进行加锁,如果多线程操作共享变量也同样采用这种方式. 为什么要对共享变量加锁或使用原子操作?如两个线程操作同一变量过程中,一 ...
- 原子操作atomic
一.原子操作:即不可再细分的操作,最小的执行单位,在操作完之前都不会被任何事件中断. 整型原子操作:对int类型的操作变成原子操作. int i = 0; ...
- 原子操作(atomic operation)
深入分析Volatile的实现原理 引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共 ...
- 【C# 线程】 atomic action原子操作|primitive(基元、原语)
概念 原子操作(atomic action):也叫primitive(原语.基元),它是操作系统用语范畴.指由若干条指令组成的,用于完成一定功能的一个过程. 原语是由若干个机器指令构成的完成某种特定 ...
- Java中的Atomic包
Atomic包的作用 方便程序员在多线程环境下,无锁的进行原子操作 Atomic包核心 Atomic包里的类基本都是使用Unsafe实现的包装类,核心操作是CAS原子操作: 关于CAS compare ...
随机推荐
- docker部署验证码项目报错:at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
如果docker部署启动报错 java.lang.NullPointerException: nullat sun.awt.FontConfiguration.getVersion(FontConfi ...
- c++设计模式概述之观察者
代码写的不够规范,目的是缩短篇幅,实际情况请不要这样做 1.概述 观察者模式,类比生活中的场景,比如看电影,观众对播放的内容有不同的反应, 再比如订阅,公众号订阅,只要你订阅了其公众号,你就会收到其推 ...
- C++11之重写说明符override和final
关于 本文代码演示环境: win10 + vs2017 一个困扰 之前MFC用的多了,发现一个问题: 子类窗口的某个函数是否重载了基类的函数.解决办法是: 打开基类的代码,一个个排查. 这只是一个具体 ...
- 【LeetCode】263. Ugly Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 牛客练习赛39 B:选点(二叉树遍历+LIS)
链接: https://ac.nowcoder.com/acm/contest/368/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262 ...
- Java程序设计基础笔记 • 【第2章 变量与数据类型】
全部章节 >>>> 本章目录 2.1 变量 2.1.1 变量的概念 2.1.2 变量的使用 2.1.3 实践练习 2.2 数据类型 2.2.1 数据类型的种类 2.2.2 ...
- MySQL数据库安装Version5.7
MySQL数据库版本: mysql-5.7.22-linux-glibc2.12-x86_64 Linux服务器系统: CentOS 7.4 64bit MySQL安装用户: mysql/aliyun ...