一种是用boost::atomic;一种直接加锁;代码很简单:

#include <boost/atomic/atomic.hpp>
#include <iostream>
#include <stdlib.h>
#include <boost/thread/mutex.hpp>
#include <pthread.h>
#include <stdio.h> 
#include <sys/time.h> 
#include <time.h> 

static int loop_num = 10000;
boost::atomic<int> a(0);
static boost::mutex mtx_;
static void* test_atomic(void* agr){
	int num = *(int*)agr;
	struct timeval t_start,t_end; 

	long cost_time = 0; 
	gettimeofday(&t_start, NULL); 
	long start = ((long)t_start.tv_sec)*1000+(long)t_start.tv_usec/1000; 

	while (num--){
		++a;
	}

	gettimeofday(&t_end, NULL); 
	long end = ((long)t_end.tv_sec)*1000+(long)t_end.tv_usec/1000; 
	cost_time = end - start; 
	std::cout << "loopnum:" << loop_num << " test_atomic cost "<< cost_time << " ms\n";
	return NULL;
}

static int b = 0;
static void* test_lock(void* agr){
	int num = *(int*)agr;

	struct timeval t_start,t_end; 

	long cost_time = 0; 
	gettimeofday(&t_start, NULL); 
	long start = ((long)t_start.tv_sec)*1000+(long)t_start.tv_usec/1000; 

	while(num--){
		{
			boost::mutex::scoped_lock lock(mtx_);
			++b;
		}
	}
	gettimeofday(&t_end, NULL); 
	long end = ((long)t_end.tv_sec)*1000+(long)t_end.tv_usec/1000; 
	cost_time = end - start; 
	std::cout  << "loopnum:" << loop_num << "test_lock cost "<< cost_time << " ms\n";
	return NULL;
}
int main(int agrc, char** argv){
	if (agrc < 2){
		std::cout << "please input num:" << std::endl;
		return 0;
	}
	pthread_t main_tid;
	int num1 = atoi((char*)argv[1]);
	int num2 = atoi((char*)argv[2]);
	if (num1 == 0){
		pthread_create(&main_tid, NULL, &test_atomic, &num2); //创建线程
    		pthread_join(main_tid, NULL);
	}
	else{
		pthread_create(&main_tid, NULL, &test_lock, &num2); //创建线程
    		pthread_join(main_tid, NULL);
	}
}
测试结果如下:

allen@allen-ThinkPad-Edge-E431:~/code$ ./test 0 1000
loopnum:10000 test_atomic cost 0 ms
allen@allen-ThinkPad-Edge-E431:~/code$ ./test 1 1000
loopnum:10000test_lock cost 0 ms
allen@allen-ThinkPad-Edge-E431:~/code$ ./test 1 100000
loopnum:10000test_lock cost 9 ms
allen@allen-ThinkPad-Edge-E431:~/code$ ./test 0 100000
loopnum:10000 test_atomic cost 2 ms

随着loop数目的增大差距越来越大;与两者实现有关,一种是临界区阻塞,一种用了compare change技术,甚至看内部实现用了汇编。

在我的笔记本结果是这样,也说明了问题。

GitHub 加速计划 / li / linux-dash
6
1
下载
A beautiful web dashboard for Linux
最近提交(Master分支:3 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐