TAG:
Using Critical Section Objects<br/>The following example shows how a thread initializes, enters, <br/>and leaves a critical section.<br/>As with the mutex example (see Using Mutex Objects), <br/>this example uses structured exception-handling syntax to ensure <br/>that the thread calls the LeaveCriticalSection function to release its ownership of the critical section object. <p></p><p>// Global variable<br/>CRITICAL_SECTION CriticalSection; </p><p>void main()<br/>{<br/> ...</p><p> // Initialize the critical section one time only.<br/> InitializeCriticalSection(&CriticalSection); </p><p> ...</p><p> // Release resources used by the critical section object.<br/> DeleteCriticalSection(&CriticalSection)<br/>}</p><p>DWORD WINAPI ThreadProc( LPVOID lpParameter )<br/>{<br/> ...</p><p> // Request ownership of the critical section.<br/> __try <br/> {<br/> EnterCriticalSection(&CriticalSection); </p><p> // Access the shared resource.<br/> }<br/> __finally <br/> {<br/> // Release ownership of the critical section.<br/> LeaveCriticalSection(&CriticalSection);<br/> }</p><p> ...</p><p>}<br/></p> (admin) |