다음을 통해 공유


Critical Section Objects

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

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 EnterCriticalSection or the TryEnterCriticalSection function to indicate that the thread has entered a critical section of code.

If another thread calls EnterCriticalSection and references the same critical section object, it is blocked until the first thread calls the LeaveCriticalSection function.

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_SECTION structure. 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 InitializeCriticalSection function.

To request ownership of a critical section, call EnterCriticalSection or TryEnterCriticalSection. To release ownership, call LeaveCriticalSection. When you finish with a critical section, call the DeleteCriticalSection function 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_SECTION structure.

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-finally structured exception-handling syntax to ensure that the thread calls LeaveCriticalSection to release the critical section object.

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.

See Also

Concepts

Synchronization