Important: |
---|
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
When multiple threads have shared access to the same data, the threads can interfere with one another.
A critical section object protects a section of code from being accessed by more than one thread. However, a critical section is limited to only one process or DLL and cannot be shared with other processes.
Critical sections work by having a thread call the EnterCriticalSectionor the TryEnterCriticalSectionfunction to indicate that the thread has entered a critical section of code.
If another thread calls EnterCriticalSectionand references the same critical section object, it is blocked until the first thread calls the LeaveCriticalSectionfunction.
A critical section can protect more than one section of code as long as the same critical section object protects each section of code.
To use a critical section, first declare a CRITICAL_SECTIONstructure. Because other critical section functions require a pointer to this structure, be sure to allocate it within the scope of all functions that are using the critical section. Then, create a handle to the critical section object by calling the InitializeCriticalSectionfunction.
To request ownership of a critical section, call EnterCriticalSectionor TryEnterCriticalSection. To release ownership, call LeaveCriticalSection. When you finish with a critical section, call the DeleteCriticalSectionfunction to release the system resources that were allocated when you initialized the critical section.
The following code example shows the prototype for the critical section functions. They all require a pointer to the CRITICAL_SECTIONstructure.
Copy Code | |
---|---|
void InitializeCriticalSection (LPCRITICAL_SECTION lpCriticalSection); void EnterCriticalSection (LPCRITICAL_SECTION lpCriticalSection); void LeaveCriticalSection (LPCRITICAL_SECTION lpCriticalSection); void DeleteCriticalSection (LPCRITICAL_SECTION lpCriticalSection); |
The following code example shows how a thread initializes, enters, and leaves a critical section. This example uses try- finallystructured exception-handling syntax to ensure that the thread calls LeaveCriticalSectionto release the critical section object.
Copy Code | |
---|---|
void func1() { // The critical section, sampleCriticalSection, should already have been // initialized from any thread of the current process using InitializeCriticalSection // Obtain ownership of the critical section EnterCriticalSection (&sampleCriticalSection); // blocks until the thread can take ownership of the critical section __try { // Your code to access the shared resource goes here } __except (EXCEPTION_EXECUTE_HANDLER) { // Your code to handle exception goes here } // Release ownership of the critical section LeaveCriticalSection (&sampleCriticalSection); } |
Note: |
---|
You must implement the class and include your implementation when you build your code. |