gpt4 book ai didi

c++ - 使用 QThreads 未正确使用 CPU 内核

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:06 24 4
gpt4 key购买 nike

使用:C++ (MinGW), Qt4.7.4, Vista (OS), intel core2vPro

我需要以完全相同的方式处理 2 个大文件。所以我想从 2 个单独的线程为 2 个单独的文件调用处理例程。 GUI 线程不做任何繁重的事情;只显示一个标签并运行一个事件循环来检查线程终止条件的发射并相应地退出主应用程序。我预计这会在某种程度上平等地利用两个内核(intel core2),但相反,我从任务管理器中看到其中一个内核的利用率很高,而另一个则不是(尽管不是每次运行代码时);处理 2 个文件所花费的时间也比处理一个文件所花费的时间多得多(我认为它应该相等或多一点,但这几乎等于在非线程中一个接一个地处理 2 个文件应用)。我能否以某种方式强制线程使用我指定的内核?

QThread* ptrThread1=new QThread;
QThread* ptrThread2=new QThread;
ProcessTimeConsuming* ptrPTC1=new ProcessTimeConsuming();
ProcessTimeConsuming* ptrPTC2=new ProcessTimeConsuming();

ptrPTC1->moveToThread(ptrThread1);
ptrPTC2->moveToThread(ptrThread2);

//make connections to specify what to do when processing ends, threads terminate etc
//display some label to give an idea that the code is in execution

ptrThread1->start();
ptrThread2->start(); //i want this thread to be executed in the core other than the one used above

ptrQApplication->exec(); //GUI event loop for label display and signal-slot monitoring

最佳答案

经常从单个机械磁盘并行读取(可能在您的情况下)不会产生任何性能提升,因为磁盘的机械磁头每次都需要旋转以寻找下一个读取位置,从而有效地使您的读取顺序的。更糟糕的是,如果有很多线程正在尝试读取,相对于顺序版本,性能甚至可能会下降,因为磁头会弹回磁盘的不同位置,因此每次都需要从停止的地方旋转回来。

通常,您最好按顺序读取文件,然后使用生产者-消费者模型并行处理它们。

关于c++ - 使用 QThreads 未正确使用 CPU 内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9872684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com