Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Wednesday, May 25, 2005

C++ thread

#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "process.h"

#ifndef _MT
#error("c/c++==code generation==use runtime library: select multithreaded")
#endif

unsigned Counter;
unsigned __stdcall ExThreadProc( void* pArguments )
{
printf( "In ExThreadProc...\n" );

while ( Counter < 1000000 )
Counter++;
Sleep(100);
_endthreadex( 0 );
return 0;
}

void __cdecl BasicThreadProc( void* pArguments )
{
printf( "In BasicThreadProc...\n" );
int a = (int)pArguments;
printf("%d\n",a);
}

int main()
{
HANDLE hThread;
unsigned threadID;
printf("Counter :%d\n",Counter);
printf( "Creating second thread...\n" );
_beginthread(BasicThreadProc,0,(void*)55);
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &ExThreadProc, NULL, 0, &threadID );
WaitForSingleObject( hThread, INFINITE );
printf( "Counter should be 1000000; it is %d\n", Counter );
CloseHandle( hThread );
return 0;
}

0 Comments:

Post a Comment

<< Home