- 论坛徽章:
- 0
|
/*
* BusyWait.c
*
* Sample code for Multithreading Applications in Win32
* This is from Chapter 3, Listing 3-1
*
* Demonstrate the effect on performance
* of using a busy loop. First call the
* worker routine with just a function call
* to get a baseline performance reading,
* then create a second thread and a
* busy loop.
*
* Build command: cl /MD busywait.c
*/
#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 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();
MTVERIFY( 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);
MTVERIFY( CloseHandle(hThrd) );
Sleep(3000);
return EXIT_SUCCESS;
} |
|