C++ ------ 互斥锁、原子操作的性能测试

atomic原子操作:是在新标准C++11,引入了原子操作的概念,并通过这个新的头文件提供了多种原子操作数据类型,例如,atomic_bool,atomic_int等等

 

测试程序

复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <time.h>

#define TEST_DATA_LENGTH 100000 //每个线程操作次数
#define THREAD_NUM 10 //线程个数

using namespace std;//引入std命名空间

mutex m;//声明互斥锁m

long n_total = 0;
long m_total = 0;
atomic<long> a_total = 0;//原子量的初始化

//在不采用互斥锁和原子类的情况下
void test_f_normal()
{
    for(int i = 0; i < TEST_DATA_LENGTH; i++)
    {
        n_total += 1;
    }
}

//使用mutex互斥锁
void test_f_mutex()
{
    for(int i = 0; i < TEST_DATA_LENGTH; i++)
    {
        m.lock();
        m_total += 1;
        m.unlock();
    }
}
//使用原子操作
void test_f_atomic()
{
    for(int i = 0; i < TEST_DATA_LENGTH; i++)
    {
        a_total += 1;
    }
}




void main()
{
    thread ts[THREAD_NUM];
    //normal mode ,result is error
    clock_t start = clock();

    for(int i = 0; i < THREAD_NUM; i++)
    {
        ts[i] = thread(&test_f_normal);
    }

    for(int i = 0; i < THREAD_NUM; i++)
    {
        ts[i].join();
    }

    clock_t end = clock();
    cout << "total: " << n_total << endl;
    cout << "test_f_normal: " << end - start << endl;


    //use mutex ,
    clock_t mstart = clock();

    for(int i = 0; i < THREAD_NUM; i++)
    {
        ts[i] = thread(&test_f_mutex);
    }

    for(int i = 0; i < THREAD_NUM; i++)
    {
        ts[i].join();
    }

    clock_t mend = clock();
    cout << "total: " << m_total << endl;
    cout << "test_f_mutex: " << mend - mstart << endl;

    //use atomic
    clock_t astart = clock();

    for(int i = 0; i < THREAD_NUM; i++)
    {
        ts[i] = thread(&test_f_atomic);
    }

    for(int i = 0; i < THREAD_NUM; i++)
    {
        ts[i].join();
    }

    clock_t aend = clock();
    cout << "total: " << a_total << endl;
    cout << "test_f_atomic: " << aend - astart << endl;

    system("pause");
    return;
}
复制代码

 

 

测试结果

total: 601409
test_f_normal: 29
total: 1000000
test_f_mutex: 11274
total: 1000000
test_f_atomic: 35

 

 

总结

由上面的测试结果可以看得出来
1.在不使用互斥锁和原子量的时候,多线程的操作会使结果是错误的.
2.原子操作的实现跟普通数据类型类似,但是它能够在保证结果正确的前提下,提供比mutex等锁机制更好的性能

提示:开发过程中,对于多线程的情况下,单个基础数据类型的数据共享安全,尽量使用原子操作代替锁机制. 当需要对代码块进行数据安全保护的时候,就需要选择使用锁机制或者自旋锁了。

 

 

posted @   流水灯  阅读(4282)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示