///////////////////////////////////////////////////////////////////////////// // Creation 16/12/2003 // // // BEGINTHREAD.C // ------------- // // // Sylvain MARECHAL - sylvain.marechal1@libertysurf.fr ///////////////////////////////////////////////////////////////////////////// // // beginthread usage // // NOTE : To use _beginthread or _beginthreadex, the application must link // with one of the multithreaded C run-time libraries // ///////////////////////////////////////////////////////////////////////////// #include #include #include #include #include //write static CRITICAL_SECTION s_CriticalSection; ///////////////////////////////////////////////////////////////////////////// // // PRINTMESSAGE ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --PrintMessage-- // ARGUMENTS // Argument1: char * pszMessage // RETOUR/RESULTAT // void // REMARQUE // Rev 16/12/2003 ////////////////////////////////////////////////////////////////////////////// void PrintMessage( char * pszMessage ) { // printf not thread safe EnterCriticalSection( &s_CriticalSection ); write( 1, pszMessage, strlen(pszMessage) ); LeaveCriticalSection( &s_CriticalSection ); } ///////////////////////////////////////////////////////////////////////////// // // THREADPROC ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --ThreadProc-- // ARGUMENTS // Argument1: void * lpParameter // RETOUR/RESULTAT // unsigned __stdcall // REMARQUE // Rev 16/12/2003 ////////////////////////////////////////////////////////////////////////////// unsigned __stdcall ThreadProc( void * lpParameter ) { HANDLE hThreadEvent = (HANDLE)lpParameter; char szBuffer[1024]; int i; PrintMessage( "Thread is created\n" ); if( ! SetEvent( hThreadEvent ) ) { sprintf( szBuffer, "SetEvent() error %d\n", GetLastError() ); PrintMessage( szBuffer ); _endthreadex( 1 ); return 1; } for( i = 0; i < 10; i ++ ) { sprintf( szBuffer, "In the thread (%d)\n", i ); PrintMessage( szBuffer ); } PrintMessage( "Prepare to exit the thread ...\n" ); _endthreadex( 0 ); return 0; } ///////////////////////////////////////////////////////////////////////////// // // MAIN ///////////////////////////////////////////////////////////////////////////// // // DESCRIPTION // --main-- // ARGUMENTS // RETOUR/RESULTAT // int // REMARQUE // Rev 16/12/2003 ////////////////////////////////////////////////////////////////////////////// int main() { HANDLE hThread, hThreadEvent; DWORD IDThread; char szBuffer[1024]; int i; DWORD dwWait; // Initialize the critical section used by printMessage InitializeCriticalSection( &s_CriticalSection ); PrintMessage( "Thread is created\n" ); // Creat a event. This event is signaled by the thread hThreadEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); if( ! hThreadEvent ) { sprintf( szBuffer, "CreateEvent() error %d\n", GetLastError() ); PrintMessage( szBuffer ); return 1; } // Create the thread, and give the event as an argument to // avoid global variable usage PrintMessage( "Before CreateThread()\n" ); hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc, (void *)hThreadEvent, 0, &IDThread ); if( ! hThread ) { sprintf( szBuffer, "CreateThread() error %d\n", GetLastError() ); PrintMessage( szBuffer ); return 1; } PrintMessage( "After CreateThread()\n" ); // Wait for the thread event PrintMessage( "Before WaitForSingleObject() - 1\n" ); dwWait = WaitForSingleObject( hThreadEvent, INFINITE ); if( dwWait != WAIT_OBJECT_0 ) { sprintf( szBuffer, "WaitForSingleObject() error %d (dwWait=%d)\n", GetLastError(), dwWait ); PrintMessage( szBuffer ); return 1; } PrintMessage( "After WaitForSingleObject() - 1\n" ); // Some message for( i = 0; i < 10; i ++ ) { sprintf( szBuffer, "In the MAIN thread (%d)\n", i ); PrintMessage( szBuffer ); } // Wait for the thread end PrintMessage( "Before WaitForSingleObject() - 2\n" ); dwWait = WaitForSingleObject( hThread, INFINITE ); if( dwWait != WAIT_OBJECT_0 ) { sprintf( szBuffer, "WaitForSingleObject() error %d (dwWait=%d)\n", GetLastError(), dwWait ); PrintMessage( szBuffer ); return 1; } PrintMessage( "After WaitForSingleObject() - 2\n" ); // Destroy crtical section DeleteCriticalSection( &s_CriticalSection ); // printf( "Prepare to exit this example ...\n" ); getch(); return 0; }