标签:
THRDTERM产生两个线程,周期性地检查一个event对象,以决定要不要结束自己。
#define WIN32_LEAN_AND_MEAN
#include<stdio.h>}
DWORD WINAPI ThreadFunc(LPVOID p)
{
int i;
int inside = 0;
UNREFERENCED_PARAMETER(p);
//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++;
}
if (WaitForSingleObject(hRequestExitEvent, 0) != WAIT_TIMEOUT)
{
printf("Received request to terminate\n");
return (DWORD)-1;
}
}
printf("PI=%.4g\n",(double)inside / i*4);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/wangfengfan1/article/details/47040819