C++互斥量、原子锁、自旋锁等比较
生活随笔
收集整理的這篇文章主要介紹了
C++互斥量、原子锁、自旋锁等比较
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
現(xiàn)象:
(1)單線程無鎖速度最快,但應(yīng)用場合受限;
(2)多線程無鎖速度第二快,但結(jié)果不對,未保護臨界代碼段;
(3)多線程原子鎖第三快,且結(jié)果正確;
(4)多線程互斥量較慢,慢與原子鎖近10倍,結(jié)果正確;
(5)多線程自旋鎖最慢,慢與原子鎖30倍,結(jié)果正確。
結(jié)論:原子鎖速度最快,互斥量和自旋鎖都用保護多線程共享資源。
? ? ? ?自旋鎖是一種非阻塞鎖,也就是說,如果某線程需要獲取自旋鎖,但該鎖已經(jīng)被其他線程占用時,該線程不會被掛起,而是在不斷的消耗CPU的時間,不停的試圖獲取自旋鎖。
? ? ? ?互斥量是阻塞鎖,當(dāng)某線程無法獲取互斥量時,該線程會被直接掛起,該線程不再消耗CPU時間,當(dāng)其他線程釋放互斥量后,操作系統(tǒng)會激活那個被掛起的線程,讓其投入運行。
? ? ? ?在多處理器環(huán)境中對持有鎖時間較短的程序來說使用自旋鎖代替一般的互斥鎖往往能提高程序的性能,但是本代碼無該效果。
#include <iostream> #include <atomic> #include <mutex> #include <thread> #include <vector>class spin_mutex {std::atomic<bool> flag = ATOMIC_VAR_INIT(false); public:spin_mutex() = default;spin_mutex(const spin_mutex&) = delete;spin_mutex& operator= (const spin_mutex&) = delete;void lock() {bool expected = false;while (!flag.compare_exchange_strong(expected, true))expected = false;}void unlock() {flag.store(false);} };long size = 1000000; long total = 0; std::atomic_long total2(0); std::mutex m; spin_mutex lock;void thread_click() {for (int i = 0; i < size; ++i){++total;} }void mutex_click() {for (int i = 0; i < size; ++i){m.lock();++total;m.unlock();} }void atomic_click() {for (int i = 0; i < size; ++i){++total2;} }void spinlock_click() {for (int i = 0; i < size; ++i){lock.lock();++total;lock.unlock();} }int main() {int thnum = 100;std::vector<std::thread> threads(thnum);clock_t start, end;total = 0;start = clock();for (int i = 0; i < size * thnum; i++) {++total;}end = clock();std::cout << "single thread result: " << total << std::endl;std::cout << "single thread time: " << end - start << std::endl;total = 0;start = clock();for (int i = 0; i < thnum; ++i) {threads[i] = std::thread(thread_click);}for (int i = 0; i < thnum; ++i) {threads[i].join();}end = clock();std::cout << "multi thread no mutex result: " << total << std::endl;std::cout << "multi thread no mutex time: " << end - start << std::endl;total = 0;start = clock();for (int i = 0; i < thnum; ++i) {threads[i] = std::thread(atomic_click);}for (int i = 0; i < thnum; ++i) {threads[i].join();}end = clock();std::cout << "multi thread atomic result: " << total2 << std::endl;std::cout << "multi thread atomic time: " << end - start << std::endl;total = 0;start = clock();for (int i = 0; i < thnum; ++i) {threads[i] = std::thread(mutex_click);}for (int i = 0; i < thnum; ++i) {threads[i].join();}end = clock();std::cout << "multi thread mutex result: " << total << std::endl;std::cout << "multi thread mutex time: " << end - start << std::endl;total = 0;start = clock();for (int i = 0; i < thnum; ++i) {threads[i] = std::thread(spinlock_click);}for (int i = 0; i < thnum; ++i) {threads[i].join();}end = clock();std::cout << "spin lock result: " << total << std::endl;std::cout << "spin lock time: " << end - start << std::endl;getchar();return 0; }/* single thread result: 100000000 single thread time: 231 multi thread no mutex result: 11501106 multi thread no mutex time: 261 multi thread atomic result: 100000000 multi thread atomic time: 1882 multi thread mutex result: 100000000 multi thread mutex time: 16882 spin lock result: 100000000 spin lock time: 45063 */?
總結(jié)
以上是生活随笔為你收集整理的C++互斥量、原子锁、自旋锁等比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NLP入门_自然语言处理_AI分支
- 下一篇: 在线网校系统搭建的意义是什么?怎么搭建?