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_COUNT; i++) { { std::unique_lock<std::mutex> lk(m); int_array[0]++; } { std::unique_lock<std::mutex> lk(m); int_array[1]--; } } } void test(void (*func)()) { atomic_array = new std::atomic<unsigned int>[2]; atomic_array[0] = 0; atomic_array[1] = LOOP_COUNT * THREAD_COUNT; int_array = new unsigned int[2]; int_array[0] = 0; int_array[1] = LOOP_COUNT * THREAD_COUNT; std::thread** tarray = new std::thread*[THREAD_COUNT]; clock_t start = clock(); for (int i = 0; i < THREAD_COUNT; i++) { tarray[i] = new std::thread(func); } for (int i = 0; i < THREAD_COUNT; i++) { tarray[i]->join(); } clock_t end = clock(); printf("cost:%dms\n", end - start); for (int i = 0; i < THREAD_COUNT; i++) { delete tarray[i]; } delete[] tarray; } int main(char* args, int size) { //为了排除测试程序的无关因素,测试时只开启一个:blocking或者nonblocking。 /*test(&atomic_fun); printf("[atomic]0:%d 1:%d\n", atomic_array[0], atomic_array[1]);*/ test(&mutex_fun); printf("[mutex]0:%d 1:%d\n", int_array[0], int_array[1]); }
两种接口测试分别跑了两次,结果如下
atomic测试结果
cost:5938ms
0:100000000 1:0
cost:5889ms
0:100000000 1:0
mutex测试结果
cost:10670ms
0:100000000 1:0
cost:11103ms
0:100000000 1:0
结论:atomic的性能比mutex高一倍,不过C++11中的mutex性能已经优化很好了,旧版编译器中mutex性能差不多比atomic低一个数量级。
测试环境:
windows 10 pro x64
VS2015企业版 update2,release x64
CPU:i7二代移动版
相关推荐
Mutex 又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文件中,所以如果你需要使用 std::mutex,就必须包含 <mutex> 头文件。 <mutex> 头文件介绍 Mutex 系列类(四种) std::mutex,...
前面两讲《C++11 并发指南二(std::thread 详解) 》,《C++11 并发指南三(std::mutex 详解) 》分别介绍了 std::thread 和 std::mutex,相信读者对 C++11 中的多线程编程有了一个最基本的认识,本文将介绍 C++11 标准...
C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装。不过里面也有很多细节值得学习。 std::mutex 先来看下std::mutex: 包增了一个pthread_mutex_t __m_,很简单,每个函数该干嘛...
c Linux平台下C++(C++98、C++03、C++11)实现的线程池 ... 最新做法,**完全采用C++11**技术,比如std::thread, mutex, condition_variable, atomic组件, 还有lambda技巧,packaged_task, future等等;
在x86_64 CPU上生成的原子操作和C ++ 11内存壁垒以及汇编程序指令
使用Qt 和 C++11 的std::mutex 和 std::condition_variable 实现一个演示生产者消费者模式的Qt工程。
线程池C++11线程池####用法: std::mutex mtx;try{tp::thread_pool tp;std::vector<std>> v;std::vector<std>> v1;for (int i = 0; i != 10; ++i){auto ans = tp.submit([](int answer) { return answer; }, i);...
Here you will learn about algebraic types such as std::optional, vocabulary types such as std::function, smart pointers, and synchronization primitives such as std::atomic and std::mutex. In the ...
C++11关于thead的应用,利用std::condition std::mutex提供如何中断,停止和继续功能,
object_threadsafe:我们将任何对象的线程安全性和std :: shared_mutex的速度提高10倍,以实现读取率> 85%的无锁算法的速度
那是: 创建线程-std :: thread, 锁定-std :: mutex,std :: condition_variable等。 时间-std :: chrono,std :: sleep_for等期货-std :: assync,std :: promise,std :: future等。 std :: notify_all_at_...
4.并发与多线程编程:掌握C++并发库(如std::thread、std::async、std::mutex等)以及多线程设计策略,确保在多核处理器环境下实现高效并发处理。 5.数据库操作:熟悉SQL语言,能使用ODBC、JDBC等数据库接口或ORM...
C++ Concurrency in Action中文 PDF清晰版 本书是并发和多线程机制指导书籍(基于C++11标准)。 从最基本的 std::thread std::mutex 和 std::async 的使用, 到 复杂的原子操作和内存模型。
This book is an in-depth guide to the concurrency ...the new C++ Standard, from the basic usage of std::thread, std::mutex, and std:: async, to the complexities of atomic operations and the memory model.
CMSIS C ++ CMSIS-RTOS API版本2的C ++ 11 / C ++ 14接口。 此源代码基于接口实现了STL的某些类。... 您可以直接使用std :: mutex , std :: timed_mutex , std :: recursive_mutex和std :: recursive_
在 《 C++11 并发指南三(std::mutex 详解) 》一文中我们主要介绍了 C++11 标准中的互斥量(Mutex),并简单介绍了一下两种锁类型。本节将详细介绍一下 C++11 标准的锁类型。 C++11 标准为我们提供了两种基本的锁类型,...
c++11多线程库中互斥库模块的使用方式,介绍了mutex类和time_mutex类的使用方式
使用C++11标准的的线程语法,用两个线程轮流打印整数,一个线程打印奇数,一个线程打印偶数。可以练习线程的基本操作、线程锁和条件变量等技术。完整代码如下。代码后面附有主要语句的讲解。 #include #include #...
std::lock_gurad 是 C++11 中定义的模板类。定义如下: template <class> class lock_guard; lock_guard 对象通常用于管理某个锁(Lock)对象,因此与 Mutex RAII 相关,方便线程对互斥量上锁,即在某个 lock_guard...