15,467
社区成员
发帖
与我相关
我的任务
分享// test_2_thread.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
HANDLE thread1_finish;
DWORD
__stdcall thread1( LPVOID para )
{
int i;
for( i = 0; i < 10; i++ )
{
printf( "%d\r\n", i );
Sleep( 500 );
}
printf( "thread1 finished\r\n" );
SetEvent( thread1_finish );
return 0;
}
DWORD
__stdcall thread2( LPVOID para )
{
WaitForSingleObject( thread1_finish, INFINITE );
printf( "thread2 finished\r\n" );
return 0;
}
int main(int argc, char* argv[])
{
HANDLE id1, id2;
thread1_finish = CreateEvent( NULL, TRUE, FALSE, NULL );
ResetEvent( thread1_finish );
id1 = CreateThread( NULL, 0, thread1, NULL, 0, NULL );
id2 = CreateThread( NULL, 0, thread2, NULL, 0, NULL );
WaitForSingleObject( id2, INFINITE );
CloseHandle( id1 );
CloseHandle( id2 );
return 0;
}