C語(yǔ)言中互斥鎖與自旋鎖及原子操作使用淺析
互斥鎖
臨界區(qū)資源已經(jīng)被1個(gè)線程占用,另一個(gè)線程過(guò)來(lái)訪問(wèn)臨界資源的時(shí)候,會(huì)被CPU切換線程,不讓運(yùn)行后來(lái)的這個(gè)線程
適用于 鎖住的內(nèi)容多,(例如紅黑數(shù)的增加節(jié)點(diǎn)操作),切換線程的代價(jià)小于等待的代價(jià)
自旋鎖
臨界區(qū)資源已經(jīng)被1個(gè)線程占用,另一個(gè)線程過(guò)來(lái)訪問(wèn)臨界資源的時(shí)候,相當(dāng)于是一個(gè) while(1)
不斷的查看這個(gè)資源是否可用,如果可用,就進(jìn)去訪問(wèn)臨界資源,如果不可用,則繼續(xù)循環(huán)訪問(wèn)
適用于鎖住的內(nèi)容少,(例如就執(zhí)行++操作),切換線程的代價(jià)大于等待的代價(jià)
原子操作
執(zhí)行的操作完全不可分割,要么全部成功,要么全部失敗
最好的方式就是適用原子操作
實(shí)操
需求場(chǎng)景:
1、用10個(gè)線程分別對(duì) count 加 100000 次, 看看結(jié)果是否是 10*100000
- main 函數(shù)中創(chuàng)建 10 個(gè)線程
- 線程函數(shù)中調(diào)用 inc 做數(shù)據(jù)的增加
- 分別使用 互斥鎖,自旋鎖,和原子操作,來(lái)進(jìn)行控制
#include <stdio.h> #include <pthread.h> #include <unistd.h> #define PTHREAD_NUM 10 #define INFO printf pthread_mutex_t mutex; pthread_spinlock_t spin; int inc(int *v,int add) { int old; //匯編,做一個(gè)原子操作 __asm__ volatile( "lock;xaddl %2, %1;" :"=a" (old) :"m"(*v),"a"(add) :"cc","memory" ); return old; } void * thread_callback(void *arg) { int *count = (int *)arg; int i = 100000; while(i--) { #if 0 //互斥鎖 pthread_mutex_lock(&mutex); (*count)++; pthread_mutex_unlock(&mutex); #elif 0 //自旋鎖 pthread_spin_lock(&spin); (*count)++; pthread_spin_unlock(&spin); #else //原子操作 inc(count,1); #endif usleep(1); } } int main() { pthread_t thread[PTHREAD_NUM] = {0}; pthread_mutex_init(&mutex,NULL); pthread_spin_init(&spin,0); int count = 0; for(int i = 0;i<PTHREAD_NUM;i++){ pthread_create(&thread[i],NULL,thread_callback,&count); } for(int i = 0;i<100;i++) { INFO("count == %d\n",count); sleep(1); } return 0; }
如上代碼還是很簡(jiǎn)單的,感興趣的 xdm 可以自行運(yùn)行,控制自己使用互斥鎖,自旋鎖或者是原子操作看看效果進(jìn)行對(duì)比一下
2、mutex、lock、atomic 性能對(duì)比
思路還是和上面的思路類型,咱們可以通過(guò)下面的代碼來(lái)實(shí)際初步看看 mutex、lock、atomic 各自的性能
//并發(fā) //互斥鎖mutex // 如果獲取不到資源會(huì)讓出cpu // 使用場(chǎng)景 // 共享區(qū)域執(zhí)行的內(nèi)容較多的情況 //自旋鎖spinlock // 如果獲取不到資源,會(huì)原地自旋,忙等 // 使用場(chǎng)景 // 共享區(qū)域執(zhí)行的內(nèi)容較少的情況 //原子操作 // 不可分割 // 使用場(chǎng)景 // 做簡(jiǎn)單++、--操作 // #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <time.h> #define MAX_PTHREAD 2 #define LOOP_LEN 1000000000 #define LOOP_ADD 10000 int count = 0; pthread_mutex_t mutex; pthread_spinlock_t spin; typedef void *(*functhread)(void *arg); void do_add(int num) { int sum = 0; for(int i = 0;i<num;i++) { sum +=i; } } int atomic_add(int *v,int add) { int old; __asm__ volatile( "lock;xaddl %2, %1;" :"=a" (old) :"m"(*v),"a"(add) :"cc","memory" ); return old; } void * atomicthread(void *arg) { for(int i = 0;i<LOOP_LEN;i++){ atomic_add(&count,1); } } void * spinthread(void *arg) { for(int i = 0;i<LOOP_LEN;i++){ pthread_spin_lock(&spin); count++; //do_add(LOOP_ADD); pthread_spin_unlock(&spin); } } void * mutexthread(void *arg) { for(int i = 0;i<LOOP_LEN;i++){ pthread_mutex_lock(&mutex); count++; //do_add(LOOP_ADD); pthread_mutex_unlock(&mutex); } } int test_lock(functhread thre,void * arg) { clock_t start = clock(); pthread_t tid[MAX_PTHREAD] = {0}; for(int i = 0;i<MAX_PTHREAD;i++) { //創(chuàng)建線程 int ret = pthread_create(&tid[i],NULL,thre,NULL); if(0 != ret) { printf("pthread create rror\n"); return -1; } } for(int i = 0;i<MAX_PTHREAD;i++){ //回收線程 pthread_join(tid[i],NULL); } clock_t end = clock(); //printf("start -- %ld\n",start); //printf("end -- %ld\n",end); //printf("CLOCKS_PER_SEC -- %ld\n",CLOCKS_PER_SEC); printf("spec lock is -- %ld\n",(end - start)/CLOCKS_PER_SEC); } int main() { pthread_mutex_init(&mutex,NULL); pthread_spin_init(&spin,0); //測(cè)試spin count = 0; printf("use spin ------ \n"); test_lock(spinthread,NULL); printf("count == %d\n",count); //測(cè)試mutex count = 0; printf("use mutex ------ \n"); test_lock(mutexthread,NULL); printf("count == %d\n",count); //測(cè)試atomic count = 0; printf("use automic ------ \n"); test_lock(atomicthread,NULL); printf("count == %d\n",count); return 0; }
結(jié)果
通過(guò)上述結(jié)果,我們可以看到,加互斥鎖,自旋鎖,原子操作,數(shù)據(jù)都能如我所愿的累加正確,在時(shí)間上面他們還是有一定的差異:
自旋鎖 和 互斥鎖 在此處的案例性能差不多,但是原子操作相對(duì)就快了很多
到此這篇關(guān)于C語(yǔ)言中互斥鎖與自旋鎖及原子操作使用淺析的文章就介紹到這了,更多相關(guān)C語(yǔ)言互斥鎖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VC++開(kāi)發(fā)中完美解決頭文件相互包含問(wèn)題的方法解析
本文中,為了敘述方便,把class AClass;語(yǔ)句成為類AClass的聲明,把class AClass開(kāi)始的對(duì)AClass的類成員變量、成員函數(shù)原型等的說(shuō)明稱為類的定義,而把在CPP中的部分稱為類的定義2013-09-09嵌入式C實(shí)戰(zhàn)項(xiàng)目開(kāi)發(fā)技巧:對(duì)一個(gè)有規(guī)律的數(shù)組表進(jìn)行位移操作的方法
今天小編就為大家分享一篇關(guān)于嵌入式C實(shí)戰(zhàn)項(xiàng)目開(kāi)發(fā)技巧:對(duì)一個(gè)有規(guī)律的數(shù)組表進(jìn)行位移操作的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12C語(yǔ)言數(shù)組實(shí)現(xiàn)打磚塊游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)組實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05詳解C語(yǔ)言如何計(jì)算結(jié)構(gòu)體大小(結(jié)構(gòu)體的內(nèi)存對(duì)齊)
結(jié)構(gòu)體的內(nèi)存對(duì)齊是有關(guān)結(jié)構(gòu)體內(nèi)容的很重要一個(gè)知識(shí)點(diǎn),主要考察方式是計(jì)算結(jié)構(gòu)體的字節(jié)大小,所以本文就給大家詳細(xì)介紹一下C語(yǔ)言如何計(jì)算結(jié)構(gòu)體大小,文中的代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07一文學(xué)會(huì)數(shù)據(jù)結(jié)構(gòu)-堆
本文主要介紹了數(shù)據(jù)結(jié)構(gòu)-堆,文中通過(guò)圖片和大量的代碼講解的非常詳細(xì),需要學(xué)習(xí)的朋友可以參考下這篇文章,希望可以幫助到你2021-08-08解決Qt設(shè)置QTextEdit行高的問(wèn)題
這篇文章介紹了Qt設(shè)置QTextEdit行高的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04C語(yǔ)言簡(jiǎn)單實(shí)現(xiàn)門禁系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言簡(jiǎn)單實(shí)現(xiàn)門禁系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01C++實(shí)現(xiàn)LeetCode(5.最長(zhǎng)回文子串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(5.最長(zhǎng)回文子串),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07