C++的互斥锁和读写锁速度比较实战

一 代码

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <cstdlib>

int gcn = 0;
pthread_mutex_t mutex;   //共享锁
pthread_rwlock_t rwlock;  //读写锁

void *thread_1(void *arg) {
    int j;
    volatile int a;
    for (j = 0; j < 10000000; j++) {
        pthread_mutex_lock(&mutex);   // 上锁
        a = gcn;  //只读全局变量gcn
        pthread_mutex_unlock(&mutex);    //解锁
    }  
    pthread_exit((void *)0);
}

void *thread_2(void *arg) {
    int j;
    volatile int b;
    for (j = 0; j < 10000000; j++) {
        pthread_mutex_lock(&mutex);   // 上锁
        b = gcn;  //只读全局变量gcn
        pthread_mutex_unlock(&mutex);    //解锁
    }  
    pthread_exit((void *)0);
}

void *thread_3(void *arg) {
    int j;
    volatile int a;
    for (j = 0; j < 10000000; j++) {
        pthread_rwlock_rdlock(&rwlock);   // 上锁
        a = gcn;  //只读全局变量gcn
        pthread_rwlock_unlock(&rwlock);    //解锁
    }  
    pthread_exit((void *)0);
}

void *thread_4(void *arg) {
    int j;
    volatile int b;
    for (j = 0; j < 10000000; j++) {
        pthread_rwlock_rdlock(&rwlock);   // 上锁
        b = gcn;  //只读全局变量gcn
        pthread_rwlock_unlock(&rwlock);    //解锁
    }  
    pthread_exit((void *)0);
}

int mutextVer(void)
{
    int j,err;
    pthread_t th1, th2;
    
    struct timeval start;
    clock_t t1,t2;
    struct timeval end;
     
    pthread_mutex_init(&mutex, NULL); //初始化互斥锁
    
    gettimeofday(&start,NULL);

    err = pthread_create(&th1, NULL, thread_1, (void *)0);
    if (err != 0) {
        printf("create new thread error:%s\n", strerror(err));
        exit(0);
    }  
    err = pthread_create(&th2, NULL, thread_2, (void *)0);
    if (err != 0) {
        printf("create new thread error:%s\n", strerror(err));
        exit(0);
    }  
       
    err = pthread_join(th1, NULL);
    if (err != 0) {
        printf("wait thread done error:%s\n", strerror(err));
        exit(1);
    }
    err = pthread_join(th2, NULL);
    if (err != 0) {
        printf("wait thread done error:%s\n", strerror(err));
        exit(1);
    }
    gettimeofday(&end,NULL);

    pthread_mutex_destroy(&mutex); //销毁互斥锁
    
    long long total_time=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);
    total_time /= 1000;
    printf("total mutex time is %lld ms\n",total_time);

    return 0;
}

int rdlockVer(void)
{
    int j,err;
    pthread_t th1, th2;
    
    struct timeval start;
    clock_t t1,t2;
    struct timeval end;
     
    pthread_rwlock_init(&rwlock, NULL); //初始化读写锁
    
    gettimeofday(&start,NULL);

    err = pthread_create(&th1, NULL, thread_3, (void *)0);
    if (err != 0) {
        printf("create new thread error:%s\n", strerror(err));
        exit(0);
    }  
    err = pthread_create(&th2, NULL, thread_4, (void *)0);
    if (err != 0) {
        printf("create new thread error:%s\n", strerror(err));
        exit(0);
    }  
       
    err = pthread_join(th1, NULL);
    if (err != 0) {
        printf("wait thread done error:%s\n", strerror(err));
        exit(1);
    }
    err = pthread_join(th2, NULL);
    if (err != 0) {
        printf("wait thread done error:%s\n", strerror(err));
        exit(1);
    }
    gettimeofday(&end,NULL);

    pthread_rwlock_destroy(&rwlock); //销毁互斥锁
    
    long long total_time=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);
    total_time /= 1000;
    printf("total rwlock time is %lld ms\n",total_time);

    return 0;
}

int main()
{
    mutextVer();
    rdlockVer();
    return 0;
}

二 运行

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
total mutex time is 505 ms
total rwlock time is 693 ms

三 说明

即使在读情况下,读写锁依然比互斥锁速度慢。那是不是说读写锁没什么作用呢?不是这样的,虽然速度上可能不如互斥锁,但并发性好,并发性对于用户体验非常重要。对于并发要求高的地方,应该优先考虑读写锁。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值