기본 콘텐츠로 건너뛰기

글

라벨이 unixC인 게시물 표시

DIAPP04_ThreadSynchronize Programming

< Critical section> CRITICAL_SECTION cs ;  //글로벌로 선언 InitializeCriticalSection ( & cs ); //메인에서 EnterCriticalSection (& cs );    // 다른 스레드들이 못 들어옴   // common or shared variable handling   ! 리브 하기전에 다른 데로 가면 엉킴. LeaveCriticalSection (& cs );   ---------------------------------------- example -------------------------------------------- #include <Windows.h> #include <stdio.h> CRITICAL_SECTION cs; DWORD __stdcall ThreadRunner( LPVOID parameter ) { ::EnterCriticalSection( &cs ); int* argument; int count; //  루프를 3번 실행한다. count = 3; //  인자를 받아온다. argument = ( int* )parameter; while( count-- > 0 ) printf( "I'm %d Thread !!\n", *argument); ::LeaveCriticalSection( &cs ); //  이 함수와 함께 쓰레드는 종료된다. return 0; } void main() { ::InitializeCriticalSection( &cs ); HANDLE handleThread[5]; int array[5] = { 0, }; int i; for( i = 0 ;...