小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

《編程之美》上控制CPU使用率曲線的題目

 bad_boy 2012-04-15
編程之美上控制CPU使用率曲線的題目 我的解答(原)
注釋掉的部分是單線程版本,如果是雙核的CPU,那么CPU使用率峰值在50%左右,因為有其他程序運行,所以可能會有點影響

考慮到雙核或者多核,于是使用多線程,主線程直接Sleep

先上截圖





下面是代碼

作者:Gods_巨蟻(博主)

#include <windows.h>
#include <cmath>
#include <process.h> //for 多線程

using namespace std;

/*
void DoSth(unsigned iDelay)
{
unsigned iStart = ::GetTickCount();
while(::GetTickCount() - iStart < iDelay);
}

int main() //單核情況
{
double x = 0.0;
double y;
while(1)
{
y = sin(x);

unsigned iDelay = static_cast<int>(500 * y) + 500;
DoSth(iDelay);
Sleep(1000 - iDelay);

x += 0.314;
if(x >= 6.28)
x = 0;
}
}
*/


unsigned __stdcall mtThreadDoSth(void * param)
{
unsigned iDelay = reinterpret_cast<int>(param);
int iStart = ::GetTickCount();
while(::GetTickCount() - iStart < iDelay);

return 0;
}

//多核情況
int main(int argc, char **argv)
{
unsigned long thd;
unsigned tid;

int cCpus = 2; //設(shè)置CPU數(shù)量
double x = 0.0;
double y;
while(1)
{
y = sin(x);
unsigned iDelay = static_cast<int>(500 * y) + 500;
for(int i = 0; i < cCpus; ++i)
{
thd = _beginthreadex(NULL,
0,
mtThreadDoSth,
(void *)iDelay,
0,
&tid);
if(thd != NULL){
CloseHandle((HANDLE)thd);
}
}
x += 0.314;
if(x >= 6.28)
x = 0;
Sleep(1000);
}

}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多