C++加线程锁相比不加线程锁的性能到底损耗了多少

测试结果

我们日常多线程编程一定用到锁,那是不是锁不冲突就不耗时了呢?
如果锁耗时,那么具体会让性能减多少呢?

经过测试,结果如下:
运行10s如下:
不加锁:303637450
加锁:171365749
比值:1.8
也就是说不加锁比加锁快了近1倍。

PS:
本人的CPU型号是:CPU型号:Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz
因为每个循环都取时间,所以测试结果本身并不能代表CPU的性能。
虽然加锁会损耗性能,但是也不见得比你“存多分数据,频繁同步”会慢。

测试程序代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/timeb.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

#include <iostream>

using namespace std;

typedef long long Int64;

static pthread_mutexattr_t  sMutexAttr;
static pthread_once_t sMutexAttrInit = PTHREAD_ONCE_INIT;
void MutexAttrInit()
{
    memset(&sMutexAttr, 0, sizeof(pthread_mutexattr_t));
    pthread_mutexattr_init(&sMutexAttr);
}

class OSMutex
{
public:
    OSMutex(){
        (void)pthread_once(&sMutexAttrInit, MutexAttrInit);
        (void)pthread_mutex_init(&fMutex, &sMutexAttr);
        m_lockingthread = 0;
        m_lockingtimes = 0;
    }
    ~OSMutex(){
        pthread_mutex_destroy(&fMutex);
    }
    void Lock(){
    (void)pthread_mutex_lock(&fMutex);
    }
    void Unlock() {
    pthread_mutex_unlock(&fMutex);
    }
    bool TryLock(){
    int theErr = pthread_mutex_trylock(&fMutex);
    if (theErr != 0)
    {
            return false;
    }
    }
private:
    pthread_mutex_t fMutex;
    pthread_t       m_lockingthread;
    int         m_lockingtimes;
};

Int64 milliseconds()
{
    struct timeval t;
    struct timezone tz;
    int theErr = ::gettimeofday(&t, &tz);
    if(theErr < 0)
    {
    printf("gettimeofday failed\n");
    }

    Int64 tmp = (Int64)t.tv_sec * 1000 + t.tv_usec / 1000;
    static Int64 last = 0x7FFFFFFFFFFFFFFFLL;
    static int count = 0;
    if((tmp - last)/10000 != 439)   
    {
        last = tmp;
        if(count != 0)
        {
            count = 0;
        }
    }
    else
    {
        count++;
        if(count > 10)
        {
            return last;
        }
        tmp = milliseconds();
    }

    return tmp;
}

int main()
{
    Int64 before = milliseconds();
    OSMutex mtx;
    Int64 cnt = 0;
    while (1) {
        Int64 now = milliseconds();
        if (now - before > 10000) {
            break;
        }
        //mtx.Lock();
        //mtx.Unlock();
        cnt ++;
    }
    cout << cnt << endl;
    return 0;
}

执行结果

[root@lh test]# g++ main26.cpp -o main26 -lpthread
[root@lh test]# ./main26 
303460399
[root@lh test]# ./main26 
303676424
[root@lh test]# ./main26 
303775529
[root@lh test]# vim main26.cpp 
#### 去掉Lock和Unlock的注释重新编译
[root@lh test]# g++ main26.cpp -o main26 -lpthread
[root@lh test]# ./main26 
172484106
[root@lh test]# ./main26 
168165401
[root@lh test]# ./main26 
173447741

原文链接: https://www.cnblogs.com/bugutian/p/13184432.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++加线程锁相比不加线程锁的性能到底损耗了多少

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/358035

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午12:04
下一篇 2023年3月2日 下午12:05

相关推荐