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

本文通过具体测试案例,对比了在多线程环境下,不使用同步机制、使用spinlock及使用qmutex进行同步时的性能表现。测试结果显示,在不同循环次数下,各种同步方式的耗时有所不同,揭示了多线程同步机制对程序性能的影响。
最低0.47元/天 解锁文章
571

被折叠的 条评论
为什么被折叠?



