#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技术,甚至看内部实现用了汇编。
在我的笔记本结果是这样,也说明了问题。
转载请注明:爱开源 » linux下mutex与atomic性能比较