중요 섹션 개체 사용
다음 예제에서는 스레드가 중요한 섹션을 초기화, 입력 및 해제하는 방법을 보여 줍니다. InitializeCriticalSectionAndSpinCount, EnterCriticalSection, LeaveCriticalSection 및 DeleteCriticalSection 함수를 사용합니다.
// Global variable
CRITICAL_SECTION CriticalSection;
int main( void )
{
...
// Initialize the critical section one time only.
if (!InitializeCriticalSectionAndSpinCount(&CriticalSection,
0x00000400) )
return;
...
// Release resources used by the critical section object.
DeleteCriticalSection(&CriticalSection);
}
DWORD WINAPI ThreadProc( LPVOID lpParameter )
{
...
// Request ownership of the critical section.
EnterCriticalSection(&CriticalSection);
// Access the shared resource.
// Release ownership of the critical section.
LeaveCriticalSection(&CriticalSection);
...
return 1;
}