[C++11]std::mutex和std::atomic的性能测试对比(benchmark)

本文通过一个8线程累加和累减的benchmark测试,比较了C++11中std::mutex和std::atomic的性能。测试结果显示std::atomic的性能大约是std::mutex的一倍。尽管如此,C++11的mutex已经显著优化,比旧版编译器中的mutex性能提升明显。测试在Windows 10 Pro x64,VS2015企业版 Update2,Release x64环境下进行,使用i7二代移动版CPU。
摘要由CSDN通过智能技术生成

key word:std::mutex、std::atomic、benchmark、performance

 

原文作者:@玄冬Wong

 

测试案例:8个线程分别执行1250万次累加和累减,累加的最终结果为10亿,累减的最终结果为0。

 

/************************************************************************/
/*测试std::mutex和std::atomic的性能对比  */
/************************************************************************/

#include <windows.h>
#include <iostream>
#include <time.h>  
#include <thread>
#include <list> 

#include <atomic>
#include <mutex>


#define LOOP_COUNT 12500000
#define THREAD_COUNT 8

std::atomic<unsigned int>* atomic_array;

std::mutex m;
unsigned int* int_array;

void atomic_fun()
{
	for (int i = 0; i < LOOP_COUNT; i++)
	{
		atomic_array[0]++;
		atomic_array[1]--;
	}
}

void mutex_fun()
{
	for (int i = 0; i < LOOP_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值