[C++11]std::atomic、boost::atomic、Interlocked三者的性能比较(benchmark)

本文通过实验对比了Windows Interlocked、std::atomic与boost::atomic在多线程环境下的性能表现,结果显示三者性能相近。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文作者:@玄冬Wong

好久没做过benchmark了,这次之所以想测试下,是怕std::atomic的效率没有windows的Interlocked性能好,测一下发现,性能差不多,Interlocked微弱的领先优势可以忽略不计。

先公布结果:三者的性能几乎相同,windows的Interlocked略好一点点

 

测试代码:

#ifdef _WIN64
#ifndef _DEBUG
#pragma comment(lib, "libboost_atomic-vc140-mt-1_60.lib")
#endif
#endif

//#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <atomic>
#include <boost/atomic.hpp> 
#include <time.h>  
#include <thread>
#include <list> 


#define MAX_THREADS 16  
#define LOOP_COUNT 10000000

volatile long g_CountWin = 0;
std::atomic<long> g_CountStd = 0;
boost::atomic_long g_CountBoost(0);

void Interlocked_fun()
{
	for (int i = 0; i < LOOP_COUNT; i++)
	{
		InterlockedIncrement((LPLONG)&g_CountWin);
	}
}

void std_atomic_fun()
{
	for (int i = 0; i < LOOP_COUNT; i++)
	{
		++g_CountStd;
	}
}

void boost_atomic_fun()
{
	for (int i = 0; i < LOOP_COUNT; i++)
	{
		++g_CountBoost;
	}
}



void test_Interlocked()
{
	std::list<std::thread*> threadlist;

	//测试Interlocked
	printf("testing Interlocked...\n");
	clock_t start = clock();
	for (int i = 0; i < MAX_THREADS; ++i)
	{
		std::thread *t1 = new std::thread((&Interlocked_fun));
		threadlist.push_back(t1);
	}
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		(*i)->join();
	}
	clock_t finish = clock();
	printf("result:%d\n", g_CountWin);
	printf("cost:%dms\n", finish - start);
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		delete(*i);
	}
}

void test_std_atomic()
{
	std::list<std::thread*> threadlist;

	//测试std::atomic
	printf("testing std::atomic...\n");
	clock_t start = clock();
	for (int i = 0; i < MAX_THREADS; ++i)
	{
		std::thread *t1 = new std::thread((&std_atomic_fun));
		threadlist.push_back(t1);
	}
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		(*i)->join();
	}
	clock_t finish = clock();
	printf("result:%d\n", g_CountStd);
	printf("cost:%dms\n", finish - start);
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		delete(*i);
	}
}

void test_boost_atomic()
{
	std::list<std::thread*> threadlist;

	//测试boost::atomic
	printf("testing boost::atomic...\n");
	clock_t start = clock();
	for (int i = 0; i < MAX_THREADS; ++i)
	{
		std::thread *t1 = new std::thread((&boost_atomic_fun));
		threadlist.push_back(t1);
	}
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		(*i)->join();
	}
	clock_t finish = clock();
	printf("result:%d\n", g_CountBoost);
	printf("cost:%dms\n", finish - start);
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		delete(*i);
	}
}

int main(char* args, int size)
{
	test_Interlocked();
	//test_std_atomic();
	//test_boost_atomic();
}

 

 

三种API的测试线程数都是16个并发线程,测试输出结果如下(跑了5次,取的平均值):

testing Interlocked...

result:160000000

cost:4926ms

 

testing std::atomic...

result:160000000

cost:4952ms

 

testing boost::atomic...

result:160000000 

cost:4949ms

 

测试环境:

boost 1.60

windows 10 pro x64

VS2015企业版 update2,release x64

CPU:i7二代移动版

 

 

std::atomicC++11引入的一个原子类型,用于实现多线程环境下的原子操作。它提供了一种线程安全的方式来访问修改共享变量。 std::atomic<T>的内部实现可以使用不同的机制,具体取决于编译器平台。一种常见的实现方式是使用硬件提供的原子指令来实现原子操作。这些指令可以确保在多线程环境下对共享变量的操作是原子的,即不会被其他线程中断。 另一种实现方式是使用互斥锁来保护共享变量的访问。当一个线程要访问共享变量时,它会先获取互斥锁,然后执行操作,最后释放互斥锁。这种方式可以确保在任意时刻只有一个线程能够访问共享变量,从而避免了竞争条件。 无论使用哪种实现方式,std::atomic都提供了一系列的成员函数来进行原子操作,包括load、store、exchange、compare_exchange等。这些函数可以保证对共享变量的操作是原子的,并且提供了不同的内存序(memory order)选项来控制操作的顺序可见性。 下面是一个示例代码,演示了如何使用std::atomic进行原子操作: ```cpp #include <iostream> #include <atomic> std::atomic<int> counter(0); void increment() { counter.fetch_add(1, std::memory_order_relaxed); } int main() { std::cout << "Counter: " << counter.load() << std::endl; std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Counter: " << counter.load() << std::endl; return 0; } ``` 这段代码创建了一个std::atomic<int>类型的counter变量,并定义了一个increment函数,该函数使用fetch_add函数对counter进行原子加一操作。在主函数中,我们创建了两个线程来同时调用increment函数,最后输出counter的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值