C++怎样控制双核CPU?
我编写的程序在单核的CPU的计算机上可以全速运行,CPU使用率达到99%。双核为什么不行?
[解决办法]
用多线程,把计算量大的部分使用不同的线程来并行处理
[解决办法]
多线程就可以了.
2个或者4个线程空转,100%马上达到
[解决办法]
双核,只有一个核满负荷,也就是50%。
C++本身没有多线程,得用系统提供的API。
[解决办法]
#include<windows.h>#include<process.h>#include<stdlib.h>void __cdecl loop(void *){for(;;){}}int main(){_begintherad(loop,0,0);_begintherad(loop,0,0);system("echo press any key to stop thread");system("pause");return 0;}
[解决办法]
充分发挥多核优势需要把每个进程绑定到一个核上:
sched_setaffinity,man自己看一下,很常用的优化手段。
[解决办法]
优势需要把每个进程绑定到一个核上:
sched_setaffinity,man自己看一下,很常用的优化手段。