欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言中互斥鎖與自旋鎖及原子操作使用淺析

 更新時(shí)間:2023年01月11日 14:57:06   作者:阿兵云原生  
今天不整GO語(yǔ)言,我們來(lái)分享一下以前寫的C語(yǔ)言代碼,來(lái)看看互斥鎖、自旋鎖和原子操作的demo,示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值

互斥鎖

臨界區(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)文章

最新評(píng)論