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 section object 避免这一段代码被超过一个的线程去访问。但是一个critical section是受限于一个进程或者一个DLL,不能够被其他进程共享、
If another thread calls EnterCriticalSection and references the same critical section object, it is blocked until the first thread calls the LeaveCriticalSection function.
当一个线程调用EnterCriticalSection 函数 引用同一个critical section 对象的时候,它就会被阻塞直到,这第一个线程调用LeaveCriticalSection函数。
A critical section can protect more than one section of code as long as the same critical section object protects each section of code.
一个critical section可以保护超过一段的代码,只要其critical section 对象保护每一段代码。
To use a critical section, you must 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.
如果要用 critical section,首先你要声明一个CRITICAL_SECTION 结构体。因为其他critical section函数要求一个指向这个结构体的指针。确保在应用critical section的所有函数作用范围之内分配这个指针。然后,调用InitializeCriticalSection 函数生成指向 the critical section object的一个句柄。
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.
为了拥有对a critical section的拥有权,我们需要调用EnterCriticalSection 函数或者TryEnterCriticalSection函数。释放该拥有权我们可以调用LeaveCriticalSection函数。当你对a critical section的处理以后,你可以调用DeleteCriticalSection函数去释放系统资源,那时你在初始化the critical section.的时候系统分配的。
The following code example shows the prototype for the critical section functions. Notice that they all require a pointer to the CRITICAL_SECTION structure.
下列代码 展示了 the critical section 函数的原型。注意他们都需要一个指向CRITICAL_SECTION 的结构体的指针!!!
void InitializeCriticalSection (LPCRITICAL_SECTION lpCriticalSection); void EnterCriticalSection (LPCRITICAL_SECTION lpCriticalSection); void LeaveCriticalSection (LPCRITICAL_SECTION lpCriticalSection); void DeleteCriticalSection (LPCRITICAL_SECTION lpCriticalSection);
The following C++ code example shows how a thread initializes, enters, and leaves a critical section. This example uses the try-finally structured exception-handling syntax to ensure that the thread calls LeaveCriticalSection to release the critical section object.
下面的C++代码历程 展示了一个线程怎样去初始化、进入和离开一个critical section。这个代码用 try-finally structured exception-handling syntax确保释放了这个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); }
文章评论(0条评论)
登录后参与讨论