日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言中互斥鎖與自旋鎖及原子操作使用淺析_C 語言

作者:阿兵云原生 ? 更新時間: 2023-02-27 編程語言

互斥鎖

臨界區資源已經被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

欄目分類
最近更新