硬件:
硬件型号:笔记本
处理器: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秒。