CPU核心未使用QThreads正确使用

ust*_*ion 6 c++ qt multithreading qthread

使用: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
Run Code Online (Sandbox Code Playgroud)

Tud*_*dor 16

通常从单个机械磁盘并行读取(并且可能在您的情况下)不会产生任何性能增益,因为磁盘的机械磁头每次都需要旋转以寻找下一个读取位置,从而有效地使您的读取顺序.更糟糕的是,如果许多线程试图读取,性能甚至可能会因顺序版本而降低,因为磁盘头被反弹到磁盘的不同位置,因此需要每次都停止旋转.

通常,您不能比按序列中的文件读取更好,然后使用生产者 - 消费者模型并行处理它们.