DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 

 

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;
}
 
 

在这里插入图片描述

有兴趣可以访问我的个站:www.stubbornhuang.com

 
posted on   DoubleLi  阅读(545)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-03-02 av_buffersrc_add_frame使用分析
2021-03-02 值得推荐的C/C++框架和库 (真的很强大)
2021-03-02 golang goroutine协程运行机制及使用详解
2019-03-02 C/C++程序中内存被非法改写的一个检测方法
2016-03-02 nginx+ffmpeg搭建rtmp转播rtsp流的flash服务器
2016-03-02 图文详解YUV420数据格式
2016-03-02 利用ffmpeg解码h264流的代码
点击右上角即可分享
微信分享提示