linux 自旋锁 vs 互斥锁

自旋锁占用的资源比较少,互斥锁占用的资源比较多

如果阻塞的时间较短,则使用自旋锁比较好,否则,使用互斥锁,

线程想要加锁的锁被占用,自旋锁则不停地等待询问锁是否被释放,互斥锁则加入等待队列,等待锁被释放。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>

int num_spin = 0;
int num_mutex = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/

pthread_spinlock_t spin;

long long timenum(){
    struct timeval tim;
    gettimeofday (&tim , NULL);
    return (long long)tim.tv_sec*1000000+tim.tv_usec;
}
void *spinlockFun1(void *arg)
{
        long long start = timenum();
        int i = 0;
        for(; i < 50; ++i)
        {
                pthread_spin_lock(&spin);
                num_spin ++;
                sleep(1);
                pthread_spin_unlock(&spin);
        }
        printf("%s, time = %lld",__func__, timenum() - start);
        printf("\n");
}


void *spinlockFun2(void *arg)
{
        long long start = timenum();
        int i = 0;
        for(; i < 50; ++i)
        {
                pthread_spin_lock(&spin);
                num_spin ++;
                sleep(1);
                pthread_spin_unlock(&spin);
        }
        printf("%s, time = %lld",__func__, timenum() - start);
        printf("\n");
}

void *mutexFun1(void *arg)
{
        long long start = timenum();
        int i = 0;
        for(; i < 50; ++i)
        {
                pthread_mutex_lock(&mutex);
                num_mutex ++;
                usleep(1);
                pthread_mutex_unlock(&mutex);
        }
        printf("%s, time = %lld",__func__, timenum() - start);
        printf("\n");
}


void *mutexFun2(void *arg)
{
        long long start = timenum();
        int i = 0;
        for(; i < 50; ++i)
        {
                pthread_mutex_lock(&mutex);
                num_mutex ++;
                usleep(1);
                pthread_mutex_unlock(&mutex);
        }
        printf("%s, time = %lld",__func__, timenum() - start);
        printf("\n");
}
int main(int argc,char *argv[])
{
    pthread_t tid1;
        pthread_t tid2;
        int err1,err2;
        /*1,initialize spin lock*/
        pthread_spin_init(&spin,PTHREAD_PROCESS_PRIVATE);
        /*2.1,create thread 1*/
        err1 = pthread_create(&tid1,NULL,spinlockFun1,NULL);
        if(0  != err1)
        {
                printf("can't create thread1\n");
        }
        /*2.2,create thread 2*/
        err2 = pthread_create(&tid2,NULL,spinlockFun2,NULL);
        if(0  != err2)
        {
                printf("can't create thread2\n");
        }

        pthread_t tid3,tid4;
        pthread_create(&tid3,NULL,mutexFun1,NULL);
        pthread_create(&tid4,NULL,mutexFun2,NULL);


        /*3.1,wait for the thread1 to end and recycle resources*/
        pthread_join(tid1,NULL);
        /*3.2,wait for the thread2 to end and recycle resources*/
        pthread_join(tid2,NULL);

        pthread_join(tid3,NULL);
        pthread_join(tid4,NULL);
        /*4,destory the spin lock*/
        pthread_spin_destroy(&spin);
        pthread_mutex_destroy(&mutex);
        printf("num_spin = %d, num_mutex = %d\n", num_spin, num_mutex);
        return 0;
}

mutexFun1, time = 3455
mutexFun2, time = 6532
spinlockFun2, time = 99007856
spinlockFun1, time = 100008063
num_spin = 100, num_mutex = 100 

上面的程序模拟了阻塞的时间长的情况,很明显互斥锁占用的时间较短,而自旋锁因为频繁的询问锁是否被释放,而锁大部分时间是被上锁的(sleep 1s模拟),导致这样的的询问很浪费CPU资源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值