spinlock同步能力测试

硬件:

    硬件型号:笔记本

    处理器:Intel Core i5-4310U CPU @ 2.00GHz(4 CPUs)

    内存:16G

    硬盘:512固态

测试方案:使用以下的代码进行比对测试(10个线程,每个线程加数10;循环10000

#include <thread>

class ThreadPara {
private:
    int counter;
    DSpinLock spinLock;

public:
    ThreadPara() : counter(0) {}

public:
    void increment() {
        std::this_thread::sleep_for(std::chrono::microseconds(1));
        DSpinLockWrapper wrapper(&spinLock);
        counter += 1;
    }
};

void threadTaskProc(ThreadPara &threadPara) {
    int i = 10;
    while (i > 0) {
        i--;
        threadPara.increment();
    }

}

void Tests::test() {
    DebugUtil::timeWriteLine("thread condition test begin");

    for (int j = 0; j < 10000; j++) {
        ThreadPara threadPara;
        std::thread threads[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = std::thread(threadTaskProc, std::ref(threadPara));
        }
        for (auto& t: threads) {
            t.join();
        }
    }

    DebugUtil::timeWriteLine("thread condition test end");

}

测试结果

运行10000次的测试结果:

1、不使用spinlock进行同步时,总耗时300秒;

2、使用spinlock进行同步时,总耗时319秒

3、使用qmutex实现synclock,进行同步时,总耗时310秒

以上的情况令人疑惑

 

运行1000次的测试结果:

1、不使用spinlock进行同步时,总耗时31秒;

2、使用spinlock进行同步时,总耗时34秒;

3、使用qmutex实现synclock,进行同步时,总耗时38秒。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值