c++11 线程原子变量
来源:互联网 发布:ubuntu开机启动程序 编辑:程序博客网 时间:2024/06/12 00:16
#include <iostream>
#include <thread>
#include <vector>
#include <functional>
#include <atomic>
void func(int& _counter)
{
for(int i=0; i<10000; i++)
++_counter;
}
void func2(std::atomic<int>& _counter)
{
for(int i=0; i<10000; i++)
++_counter;
}
int main()
{
//int counter = 0;
std::atomic<int> _counter(0); //使用原子变量
std::vector<std::thread> _threads;
for(int i=0; i<10; i++)
_threads.push_back(std::thread{func2, std::ref(_counter)}); //要符合线程初始逻辑
for(auto& _t : _threads)
{
_t.join(); //防止主线程退出
}
std::cout<<"result:="<< _counter<<std::endl;
return 0;
}
#include <thread>
#include <vector>
#include <functional>
#include <atomic>
void func(int& _counter)
{
for(int i=0; i<10000; i++)
++_counter;
}
void func2(std::atomic<int>& _counter)
{
for(int i=0; i<10000; i++)
++_counter;
}
int main()
{
//int counter = 0;
std::atomic<int> _counter(0); //使用原子变量
std::vector<std::thread> _threads;
for(int i=0; i<10; i++)
_threads.push_back(std::thread{func2, std::ref(_counter)}); //要符合线程初始逻辑
for(auto& _t : _threads)
{
_t.join(); //防止主线程退出
}
std::cout<<"result:="<< _counter<<std::endl;
return 0;
}
0 0
- c++11 线程原子变量
- 线程4:原子变量与原子操作
- 线程下变量-原子操作 __sync_fetch_and_add
- Java 线程同步之原子变量
- 线程下变量-原子操作 __sync_fetch_and_add等等
- C++,多线程:原子操作解决线程冲突
- 原子变量
- 原子变量
- Java并发编程规则:原子变量实现线程安全
- C++11 多线程编程 原子变量
- 原子变量和原子操作
- Java原子变量&原子操作
- 原子变量的操作
- 原子变量AtomicInteger
- java 原子变量AtomicInteger
- Atomic Variables 原子变量
- Atomic Variables 原子变量
- java原子变量
- python创建型设计模式——建造者模式
- 51nod 1095 Anigram单词
- Spark性能优化之一
- 浅谈C/C++中的static和extern关键字
- 利用Java IO中的字节流与缓冲流及单个或批量读写字节进行组合测试文件复制的速度....速度差别太大了!!!
- c++11 线程原子变量
- 使用Altium Designer进行DDR2的PCB设计
- 几种负载均衡技术的实现
- rpmbuild之spec文件说明
- 大龄程序员的发展方向
- Spring声明式事务的配置方式
- leetcode-456-Convert a Number to Hexadecimal
- MySQL Server架构图
- 用eclipse创建动态web项目手动生成web.xml方法