#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <assert.h>
#include <iostream>
using namespace std;
#define US_PER_MS 1000
#define MS_PER_SEC 1000
#define SPEED 60000000UL
void Thread()
{
BOOL b = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) ;
assert(b);
b = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
assert(b);
DWORD d = SetThreadAffinityMask(GetCurrentThread(), 0x00000002);
assert(d);
INT64 try00 = SPEED;
int ms;
clock_t s, e;
while (1)
{
s = clock();
while (try00--);
e = clock();
ms = (e - s) * MS_PER_SEC / CLOCKS_PER_SEC;
Sleep(ms);
try00 = SPEED;
}
}
int main(int argc, char **argv)
{
HANDLE hThread;
DWORD ThreadID;
hThread=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread, NULL, 0, &ThreadID);
if (NULL != ThreadID)
{
WaitForSingleObject(hThread, INFINITE); //用于等待线程1安全结束
}
exit(0);
}
本人为4核机器,只有第二个cpu显示占用百分之五十,启动4个进程,感觉cpu就很混乱,占用就不规律了。希望大神补充下多核均占用百分之五十的代码。