今天有人问我windows xp时间片是多少。想了一会没想起来。索性就写了段代码。
注:是在虚拟机里单核CPU里跑的。
#include <windows.h>
#include <iostream.h>DWORD WINAPI Fun1Proc(LPVOID lpParameter);
int index = 0;void main()
{
HANDLE hThread1;
hThread1 = CreateThread(NULL, 0, Fun1Proc, NULL, 0, NULL);
CloseHandle(hThread1);
cout << "Main thread is running!" << endl;
Sleep(1000);
cout << index << endl;
cout << "Main Thread finished!" << endl;
}//线程1的入口函数
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while (index++ < 1000)
     cout << "Thread1 is running!" << endl;
return 0;
}
思路:建立两个线程,一个主一个从。然后调用Sleep函数让主线程暂停1s,执行从进程。最后打印输出。粗略的计算了下每次线程调用的时间大概是5ms左右。
大家看看这个思路是否可行。