Critical Section Object

Source: Internet
Author: User

Critical section Object from MSDN


Critical Section Objects


A critical section object provides synchronization similar to the provided by a Mutexes object, except that a critical sect Ion can is used only by the threads of a single process.
Event, mutex, and semaphore objects can also is used in a single-process application,
But critical sections objects provide a slightly faster, more efficient mechanism for mutual-exclusion synchronization (a P Rocessor-specific test and set instruction).
Like a mutex object, a critical section object can owned by only one thread at a time, which makes it useful for Protec Ting a shared resource from simultaneous access.
Unlike a mutex object, there is no-it-to-tell whether a critical-section has been abandoned.

 
 critical Section object provides a synchronization mechanism. Similar to the mutex object. However, the Critical section object object can only be used in a single process.
 Event,Mutex  and semaphore object can only be used in a single-process program.
  But the critical section Object provides a lightweight, fast, and efficient mechanism to synchronize concurrent executions.
  Type in mutex object, Critical section Object can only be owned by one thread at a time. Concurrent access to shared resources is useful.
  Unlike mutex object, there is no test of whether a critical section object is discarded.
 
Starting with the Windows Server 2003 with Service Pack 1 (SP1), threads waiting on a critical sections do not AC Quire the critical section on a first-come, First-serve basis.
This change increases performance significantly to most code.
However, some applications depend on first-in, first-out (FIFO) ordering and could perform poorly or not @ all on Curre NT versions of Windows.
(for example, applications that has been using critical sections as a rate-limiter).

Starting with Windows Server2003, threads get critical section Object without first-come-first-served, this change greatly increases the code dragons.
Then, some applications rely on FIFO first order to perform inefficiently.

To ensure this your code continues to work correctly, the need to add a additional level of synchronization.
For example, suppose you has a producer thread and a consumer thread that is using a critical section object to Synchron ize their work.

Create the event objects, one for each thread to the signal, which is the it's ready for the other thread to proceed.
The consumer thread would wait for the producer to signal it event before entering the critical section, and the producer Thread would wait for the consumer thread to signal it event before entering the critical section.
After each of the thread leaves the critical section, it signals their event to release the other thread.

Windows Server 2003 and Windows XP:
Threads that is waiting on a critical section is added to a wait queue;
They is woken and generally acquire the critical section in the order in which they were to the queue.
However, if threads is added to this queue at a fast enough rate, performance can is degraded because of the time it take s to awaken each waiting thread.

In Windows Server2003 and Windows XP:
Threads waiting for the critical section object object are joined to the wait queue.
The thread is then woken up in the order in which it waits in the queue. However, if the speed is now added to the queue fast enough, the queue time in queues is much less than the time the thread was awakened.

Test code:

#include <windows.h> #include <iostream> #include <vector>using namespace std; #define ThreadCount 9critical_section Testcs;dword WINAPI threadfunc (LPVOID);std::vector<int> ordervec;//record thread invocation order int main (void) { HANDLE Athread[threadcount];D word threadid;int i;initializecriticalsection (&AMP;TESTCS);//Create worker threadsfor ( i=0; i < ThreadCount; i++) {Athread[i] = CreateThread (NULL,//default security Attributes0,//default stack Sizethreadfunc,// (Lpthread_start_routine) (LPVOID) I,//no thread function ARGUMENTS0,//default creation Flags&threadid); Receive thread identifier}sleep (i=0; i < threadcount; i++) CloseHandle (athread[i]); int count = Ordervec.si Ze (); cout << "Order of Calling Thread:"; for (int i=0; i<count; ++i) {cout << ordervec[i] << ",";} DeleteCriticalSection (&testcs); System ("pause"); return 0;} DWORD WINAPI ThreadFunc (lpvoid lpparam) {//All threads that access Ordervec objects will be in critical Section Object is queued in FIFO order.//When there is no critical section, the Ordervec order will be disorderly. EnterCriticalSection (&AMP;TESTCS); int i = (int) lpparam;o Rdervec.push_back (i); Ordervec.push_back (i); Ordervec.push_back (i); LeaveCriticalSection (&testcs); return TRUE; }



Test results:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Critical Section Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.