C++11/std::atomic – 原子变量(不加锁实现线程互斥)
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++11/std::atomic – 原子变量(不加锁实现线程互斥)
原文链接:https://www.stubbornhuang.com/781/
发布于:2020年04月02日 13:28:17
修改于:2020年04月02日 13:29:39

1 原子操作
原子操作:一个独立不可分割的操作。多线程编程需要保证线程安全,而线程安全一个很重要的特性就是原子性,即在同一时刻只有一个线程对原子进行操作,保证数据访问的互斥性。
2 C++11原子变量
C++11提供了原子类型std::atomic
3 使用原子变量
3.1 没有使用线程互斥的数据操作
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <chrono>
long long globalCount = 0;
void ThreadFunction()
{
for (int i=0;i<100000;++i)
{
globalCount += 1;
}
}
int main()
{
std::vector<std::thread> threads;
std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();
for (int i = 0; i < 10; ++i)
{
threads.push_back(std::thread(ThreadFunction));
}
for (int i=0;i<10;++i)
{
threads[i].join();
}
std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();
std::cout << "当前总数为:" << globalCount << std::endl;
std::cout << "消耗时间为:" << std::chrono::duration_cast<std::chrono::milliseconds> (endTime - startTime).count() <<"毫秒"<< std::endl;
getchar();
return 0;
}
3.2 使用互斥量保证线程互斥
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <chrono>
long long globalCount = 0;
std::mutex globalMutex;
void ThreadFunction()
{
std::lock_guard<std::mutex> lock(globalMutex);
for (int i=0;i<100000;++i)
{
globalCount += 1;
}
}
int main()
{
std::vector<std::thread> threads;
std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();
for (int i = 0; i < 10; ++i)
{
threads.push_back(std::thread(ThreadFunction));
}
for (int i=0;i<10;++i)
{
threads[i].join();
}
std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();
std::cout << "当前总数为:" << globalCount << std::endl;
std::cout << "消耗时间为:" << std::chrono::duration_cast<std::chrono::milliseconds> (endTime - startTime).count() <<"毫秒"<< std::endl;
getchar();
return 0;
}
3.3 使用原子量std::atomic保证数据互斥
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <chrono>
std::atomic<long> globalCount = 0;
void ThreadFunction()
{
for (int i=0;i<100000;++i)
{
globalCount += 1;
}
}
int main()
{
std::vector<std::thread> threads;
std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();
for (int i = 0; i < 10; ++i)
{
threads.push_back(std::thread(ThreadFunction));
}
for (int i=0;i<10;++i)
{
threads[i].join();
}
std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();
std::cout << "当前总数为:" << globalCount << std::endl;
std::cout << "消耗时间为:" << std::chrono::duration_cast<std::chrono::milliseconds> (endTime - startTime).count() <<"毫秒"<< std::endl;
getchar();
return 0;
}
当前分类随机文章推荐
- GCC/GG++中编译优化选项-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -Oz各自的区别和作用 阅读3106次,点赞2次
- C++ - 使用Crypto++/CryptoPP加解密库对字符串或者文件进行AES256加密 阅读2979次,点赞1次
- C++STL容器 - std::vector元素访问方式总结 阅读767次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读796次,点赞0次
- C++ 11 - final关键字简要介绍 阅读1881次,点赞0次
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读3863次,点赞2次
- C++ - RAII机制 阅读242次,点赞0次
- C++ - const修饰符与指针 阅读266次,点赞1次
- C++STL容器 - std::map查找元素与判断键值是否存在方法总结 count,find,contains,equal_range,lower_bound,upper_bound 阅读929次,点赞0次
- C++ - 在两个互有依赖关系的类中使用std::shared_ptr和std::weak_ptr进行内存管理 阅读596次,点赞0次
全站随机文章推荐
- C++11/std::condition_variable - 生产者消费者模型 阅读2720次,点赞0次
- 资源分享 - AI Game Engine Programming , Second Edition 英文高清PDF下载 阅读901次,点赞0次
- 资源分享 - Texturing and Modeling - A Procedural Approach, Third Edition 英文高清PDF下载 阅读1858次,点赞0次
- 资源分享 - Calculus for Computer Graphics , Second Edition 英文高清PDF下载 阅读963次,点赞0次
- WordPress - 非管理员登录后台直接重定向到指定页面 阅读796次,点赞0次
- Gdi+ - 将OpenCV Mat转换为Gdi+ Bitmap 阅读1914次,点赞0次
- 计算几何 - C++计算两个二维向量的夹角 阅读3860次,点赞3次
- 深度学习 - 归纳轻量级神经网络(长期更新) 阅读79次,点赞0次
- Youtube运营 - 申请开通YPP(Youtube合作伙伴计划)时,人工审核未通过,理由为再利用他人的内容 阅读136次,点赞0次
- 资源分享 - Data Structures and Algorithms for Game Developers 英文高清PDF下载 阅读1519次,点赞0次
评论
167