網站首頁 編程語言 正文
互斥鎖
臨界區資源已經被1個線程占用,另一個線程過來訪問臨界資源的時候,會被CPU切換線程,不讓運行后來的這個線程
適用于 鎖住的內容多,(例如紅黑數的增加節點操作),切換線程的代價小于等待的代價
自旋鎖
臨界區資源已經被1個線程占用,另一個線程過來訪問臨界資源的時候,相當于是一個 while(1)
不斷的查看這個資源是否可用,如果可用,就進去訪問臨界資源,如果不可用,則繼續循環訪問
適用于鎖住的內容少,(例如就執行++操作),切換線程的代價大于等待的代價
原子操作
執行的操作完全不可分割,要么全部成功,要么全部失敗
最好的方式就是適用原子操作
實操
需求場景:
1、用10個線程分別對 count 加 100000 次, 看看結果是否是 10*100000
- main 函數中創建 10 個線程
- 線程函數中調用 inc 做數據的增加
- 分別使用 互斥鎖,自旋鎖,和原子操作,來進行控制
#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;
//匯編,做一個原子操作
__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;
}
如上代碼還是很簡單的,感興趣的 xdm 可以自行運行,控制自己使用互斥鎖,自旋鎖或者是原子操作看看效果進行對比一下
2、mutex、lock、atomic 性能對比
思路還是和上面的思路類型,咱們可以通過下面的代碼來實際初步看看 mutex、lock、atomic 各自的性能
//并發
//互斥鎖mutex
// 如果獲取不到資源會讓出cpu
// 使用場景
// 共享區域執行的內容較多的情況
//自旋鎖spinlock
// 如果獲取不到資源,會原地自旋,忙等
// 使用場景
// 共享區域執行的內容較少的情況
//原子操作
// 不可分割
// 使用場景
// 做簡單++、--操作
//
#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++)
{
//創建線程
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);
//測試spin
count = 0;
printf("use spin ------ \n");
test_lock(spinthread,NULL);
printf("count == %d\n",count);
//測試mutex
count = 0;
printf("use mutex ------ \n");
test_lock(mutexthread,NULL);
printf("count == %d\n",count);
//測試atomic
count = 0;
printf("use automic ------ \n");
test_lock(atomicthread,NULL);
printf("count == %d\n",count);
return 0;
}
結果
通過上述結果,我們可以看到,加互斥鎖,自旋鎖,原子操作,數據都能如我所愿的累加正確,在時間上面他們還是有一定的差異:
自旋鎖 和 互斥鎖 在此處的案例性能差不多,但是原子操作相對就快了很多
原文鏈接:https://blog.csdn.net/m0_37322399/article/details/128592386
相關推薦
- 2022-10-16 Python?讀取?Word?文檔操作_python
- 2022-04-24 Android掛斷電話最新實現方法_Android
- 2022-03-26 C++成員解除引用運算符的示例詳解_C 語言
- 2022-11-22 docker+Nginx部署前端項目的詳細過程記錄_docker
- 2022-05-06 Python如何判斷字符串是否僅包含數字_python
- 2022-03-23 C++函數模板的使用詳解_C 語言
- 2023-01-05 C++模板?index_sequence使用示例詳解_C 語言
- 2022-08-13 Spring中@Bean注解的作用以及如何使用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支