下面从一个问题引入:

// 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解读的更多相关文章

  1. 并发编程之原子操作Atomic&Unsafe

    原子操作:不能被分割(中断)的一个或一系列操作叫原子操作. 原子操作Atomic主要有12个类,4种类型的原子更新方式,原子更新基本类型,原子更新数组,原子更新字段,原子更新引用.Atomic包中的类 ...

  2. java并发:线程同步机制之Volatile关键字&原子操作Atomic

    volatile关键字 volatile是一个特殊的修饰符,只有成员变量才能使用它,与Synchronized及ReentrantLock等提供的互斥相比,Synchronized保证了Synchro ...

  3. 什么是Java中的原子操作( atomic operations)

    1.啥是java的原子性 原子性:即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行. 一个很经典的例子就是银行账户转账问题: 比如从账户A向账户B转1000元,那么 ...

  4. golang中的原子操作atomic包

    1. 概念 原子操作 atomic 包 加锁操作涉及到内核态的上下文切换,比较耗时,代价高, 针对基本数据类型我们还可以使用原子操作来保证并发的安全, 因为原子操作是go语言提供的方法,我们在用户态就 ...

  5. C++11并发编程:原子操作atomic

    一:概述 项目中经常用遇到多线程操作共享数据问题,常用的处理方式是对共享数据进行加锁,如果多线程操作共享变量也同样采用这种方式. 为什么要对共享变量加锁或使用原子操作?如两个线程操作同一变量过程中,一 ...

  6. 原子操作atomic

    一.原子操作:即不可再细分的操作,最小的执行单位,在操作完之前都不会被任何事件中断. 整型原子操作:对int类型的操作变成原子操作.                 int i = 0;       ...

  7. 原子操作(atomic operation)

    深入分析Volatile的实现原理 引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共 ...

  8. 【C# 线程】 atomic action原子操作|primitive(基元、原语)

    概念 原子操作(atomic action):也叫primitive(原语.基元),它是操作系统用语范畴.指由若干条指令组成的,用于完成一定功能的一个过程.  原语是由若干个机器指令构成的完成某种特定 ...

  9. Java中的Atomic包

    Atomic包的作用 方便程序员在多线程环境下,无锁的进行原子操作 Atomic包核心 Atomic包里的类基本都是使用Unsafe实现的包装类,核心操作是CAS原子操作: 关于CAS compare ...

随机推荐

  1. 分布式NoSQL数据库MongoDB初体验-v5.0.5

    概述 定义 MongoDB官网 https://www.mongodb.com/ 社区版最新版本5.0,其中5.2版本很快也要面世了 MongoDB GitHub源码 https://github.c ...

  2. Spring Boot应用程序启动器

    官网地址:https://docs.spring.io/spring-boot/docs/2.1.12.RELEASE/reference/html/using-boot-build-systems. ...

  3. git 添加.gitignore文件不生效

    git rm -r --cached . #新增的忽略文件没有生效,是因为git是有缓存的,而之前的文件在缓存中,并不会清除掉,还会继续提交,所以更新.gitignore文件,要清除缓存文件 git ...

  4. JAVA获取访问者的内网IP地址

    /** * 获取访问者内网IP * @return the server ip */ public static String getIntranetIp() { // 本地IP,如果没有配置外网IP ...

  5. c++设计模式概述之状态

    代码写的不够规范,目的是为了缩短篇幅,实际中请不要这样做 参看:https://www.runoob.com/design-pattern/state-pattern.html 1.概述 这个有点抽象 ...

  6. 【LeetCode】441. Arranging Coins 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟计算 二分查找 数学公式 日期 题目地址:htt ...

  7. Pikachu漏洞练习-SQL-inject(三)

  8. 教你Python字符串的基本操作:拆分和连接

    摘要:由于字符串数据几乎无处不在,因此掌握有关字符串的交易工具非常重要.幸运的是,Python 使字符串操作变得非常简单,尤其是与其他语言甚至旧版本的 Python 相比时. 本文分享自华为云社区&l ...

  9. [Git]解决Permission denied, please try again问题

    在gitlab上传项目的时候出现Permission denied, please try again问题, 网上有很多解释,但是都没能解决我的问题,后来经过自己尝试成功了,这里把经验分享给大家. 在 ...

  10. Hangfire任务调度框架使用

    1.HangFire简介 HangFire是一个免费简单实用的分布式后台定时调度服务,在现在.net开发中,人气算是很高的. HangFire提供了内置集成化的控制台,可以直观明了的查看作业调度情况, ...