QT开发中文网

win10 多核CPU 情况下 QueryPerformanceCounter计算出时间不准确 ,多线程编程,均匀的将线程绑定到每个核心上,写一个C代码例子

首先,多核CPU下的QueryPerformanceCounter计算时间不准确可能是由于以下原因导致的:

  1. 多个线程在不同核心上运行,导致计时器频率不一致;
  2. CPU节能模式或者超线程技术影响了计时器精度。

为了解决这个问题,可以使用线程亲和性(Thread Affinity)来将每个线程绑定到特定的CPU核心上,以保证计时器频率的一致性。下面是一个C代码例子,展示如何均匀地将线程绑定到每个核心上:

#include <stdio.h>
#include <windows.h>
#include <process.h>

#define NUM_THREADS 4

LARGE_INTEGER startingTime;
DWORD_PTR threadAffinityMask[] = { 0x01, 0x02, 0x04, 0x08 };

DWORD WINAPI workerThread(LPVOID lpParam) {
    int id = (int)lpParam;
    LARGE_INTEGER endingTime;
    QueryPerformanceCounter(&endingTime);
    printf("Thread %d: elapsed time = %lld\n", id, endingTime.QuadPart - startingTime.QuadPart);
    return 0;
}

int main() {
    HANDLE threads[NUM_THREADS];
    int i;

    // Set the affinity mask for each thread
    for (i = 0; i < NUM_THREADS; i++) {
        threads[i] = CreateThread(NULL, 0, workerThread, (LPVOID)i, 0, NULL);
        if (threads[i] == NULL) {
            printf("Error creating thread %d\n", i);
            return 1;
        }
        SetThreadAffinityMask(threads[i], threadAffinityMask[i]);
    }

    // Wait for all threads to complete
    WaitForMultipleObjects(NUM_THREADS, threads, TRUE, INFINITE);

    // Clean up thread handles
    for (i = 0; i < NUM_THREADS; i++) {
        CloseHandle(threads[i]);
    }

    return 0;
}

在这个例子中,我们创建了4个线程并将它们分别绑定到CPU的4个核心上。每个线程执行workerThread函数,在该函数中使用QueryPerformanceCounter函数计算从程序开始到当前时间经过的时间,然后输出结果。最后,等待所有线程完成并清理线程句柄。

通过将每个线程绑定到特定的CPU核心上,可以保证计时器频率的一致性,从而解决多核CPU下QueryPerformanceCounter计算时间不准确的问题。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=385

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?