现在的位置: 首页 > 综合 > 正文

GetExitCodeThread(hThrd, &exitCode);WaitForSingleObject(hThrd,INFINITE);

2013年10月10日 ⁄ 综合 ⁄ 共 1405字 ⁄ 字号 评论关闭
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include "..\MtVerify.h"

DWORD WINAPI ThreadFunc(LPVOID);

int g_j=100;

int main()
{
    HANDLE hThrd;BOOL bAns;//unsigned int i;
    DWORD exitCode = 0;
    DWORD threadId;
    DWORD begin,end;
    DWORD elapsed;

    puts("Busy2 is designed to use up CPU time.");
    puts("Press Ctrl+C to exit.");
    begin = GetTickCount();
	printf("begin:%d\n",begin);

    MTVERIFY( hThrd = CreateThread(NULL,
        0,
        ThreadFunc,
        (LPVOID)&g_j,
        0,
        &threadId )
    );
	printf("hThrd =:%d\n",hThrd );

    /* This busy loop chews up lots of CPU time 
    for (i=0;;i++)
    {
        GetExitCodeThread(hThrd, &exitCode);
	//	printf("i = %d\n",i );
        if ( exitCode != STILL_ACTIVE )
            break;
    }
	*/
	WaitForSingleObject(hThrd,INFINITE);

	end=GetTickCount();
    elapsed = end-begin;
	printf("begin:%d,end:%d,elapsed:%d\n",begin,end,elapsed);

    printf("Thread + busy loop took: %d.%.03d seconds\n",
                elapsed/1000, elapsed%1000);

 MTVERIFY( (bAns=CloseHandle(hThrd)) );
 printf("hThrd =:%d,bAns=%d\n",hThrd ,bAns);

    return EXIT_SUCCESS;
}


/*
 * Cute little busy work routine that computes the value
 * of PI using probability.  Highly dependent on having
 * a good random number generator (rand is iffy)
 */
DWORD WINAPI ThreadFunc(LPVOID n)
{
    int i;
    int inside = 0;
    double val;

    //UNREFERENCED_PARAMETER(n);
	printf("g_j:%d\n",*(int*)n);

    /* Seed the random-number generator */
    srand( (unsigned)time( NULL ) );

    for (i=0; i<100000000; 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;
}

【上篇】
【下篇】

抱歉!评论已关闭.