这是第三章里面的示例程序(经过修改,去掉了MTVERIFY宏):
代码如下:#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>DWORD WINAPI ThreadFunc(LPVOID n)
{
int i;
int inside = 0;
double val; UNREFERENCED_PARAMETER(n); /* Seed the random-number generator */
srand( (unsigned)time( NULL ) ); for (i=0; i<1000000; i++)
{
double x = (double)(rand())/RAND_MAX;
double y = (double)(rand())/RAND_MAX;
if ( (x*x + y*y) <= 1.0 )
inside++;
}
val = (double)inside / i;
printf("PI = %.4g\n", val*4);
return 0;
}int main()
{
HANDLE hThrd;
DWORD exitCode = 0;
DWORD threadId;
DWORD begin;
DWORD elapsed; puts("Timing normal function call...");
begin = GetTickCount();
ThreadFunc(0);
elapsed = GetTickCount()-begin;
printf("Function call took: %d.%.03d seconds\n\n",
elapsed/1000, elapsed%1000); puts("Timing thread + busy loop...");
begin = GetTickCount(); hThrd = CreateThread(NULL,
0,
ThreadFunc,
(LPVOID)1,
0,
&threadId );

/* This busy loop chews up lots of CPU time */
for (;;)
{
GetExitCodeThread(hThrd, &exitCode);
if ( exitCode != STILL_ACTIVE )
break;
} elapsed = GetTickCount()-begin;
printf("Thread + busy loop took: %d.%.03d seconds\n",
elapsed/1000, elapsed%1000); CloseHandle(hThrd); return EXIT_SUCCESS;
}问题是貌似Timing thread + busy loop...应该耗时多一些,可是实验结果是Timing thread + busy loop...有可能耗时更少,这是问什么?哪位知道原因,告知一下,不胜感激啊!我的系统是Windows Xp SP3,使用的工具是Visual Studio 2008. CPU为Intel T2300 @1.66GHz(很早的笔记本上的酷睿双核)。