VC Multi-threaded Critical Zone (RPM)

Source: Internet
Author: User

Used to understand criticalsection. When using multi-threading, there are generally few threads that work completely independently. It is often that multiple threads manipulate a global variable at the same time to obtain the running results of the program. Multiple threads access the same global variable at the same time, and if all are read operations, the problem does not occur. If it is a write operation, an error occurs.       At this point, we can set a protection for the global variable through the critical section, guaranteeing that only one thread can access the variable, and the other variables enter the wait state. Critical area (Critical section)is a piece of code that exclusively accesses access to certain shared resources, only one thread is allowed to access the shared resource at any time. If more than one thread attempts to access the critical section at the same time, all other threads that attempt to access this critical section after one thread has entered will be suspended and continue until the thread entering the critical section leaves.       When the critical section is released, other threads can continue to preempt, and in this way achieve the purpose of atomic manipulation of shared resources. The critical section protects the shared resources with the CRITICAL_SECTION structure object and uses ①initializecriticalsection (&AMP;CS) respectively. Initialize Critical zone ②entercriticalsection (&AMP;CS);//Enter critical section ③leavecriticalsection (&AMP;CS);//Leave critical area ④deletecriticalsection ( &AMP;CS);//Delete critical section General class program (example) //Critical section Structure object Critical_section G_cs;//Shared resources Charg_carray[10] UINT ThreadProc1 (LPVOID pparam){//Enter the critical section EnterCriticalSection (&G_CS);//Write to a shared resource   For(IntI= 0; I< 10; I++){G_carray[i]=' A '; Sleep (1); }   //Leave the critical section LeaveCriticalSection (&G_CS);Return 0; }UINT ThreadProc2 (LPVOID pparam){//Enter the critical section EnterCriticalSection (&G_CS);//Write to a shared resource   For(IntI= 0; I< 10; I++){g_carray[10 -I- 1]=' B '; Sleep (1); }   //Leave the critical section LeaveCriticalSection (&G_CS);Return 0; } voidCsampleview::oncriticalsection (){//Initialize critical section initializecriticalsection (&G_cs);  // start thread afxbeginthread (ThreadProc1, NULL);  AfxBeginThread (THREADPROC2, NULL);  // wait for the calculation to complete Sleep (+);  // report calculation results CString sresult = CString (g_carray); AfxMessageBox (Sresult); }when using a critical section, it is generally not allowed to run long, as long as the thread entering the critical section has not left, all other threads attempting to enter this critical section will be suspended and go to the waiting state, and will be affected to some extent. The running performance of the program. In particular, it is important not to include operations that wait for user input or some other external intervention into the critical section. If you enter a critical section and you have not released it, it will also cause other threads to wait for a long time. In other words, no matter what happens after executing the entercriticalsection () statement into the critical section, you must ensure that the matching leavecriticalsection () can be executed. You can ensure the execution of the LeaveCriticalSection () statement by adding structured exception handling code. Although critical section synchronization is fast, it can only be used to synchronize threads within this process and not to synchronize threads in multiple processes.

MFC provides a CCriticalSection class for critical sections, and it is very simple to use this class for thread synchronization, with the CCriticalSection class member function, Lock (), and unlock () in the threads function to demarcate the protected code fragment. For the above code, it can be rewritten by the CCriticalSection class as follows:

MFC Applications (example)

//MFC critical section Class object CCriticalSection G_cs;//Shared resources Charg_carray[10]; UINT ThreadProc1 (LPVOID pparam){//Enter the critical section G_cs. Lock ();//Write to a shared resource   For(IntI= 0; I< 10; I++){G_carray[i]=' A '; Sleep (1); }   //Leave the critical section G_cs. Unlock ();Return 0; }UINT ThreadProc2 (LPVOID pparam){//Enter the critical section G_cs. Lock ();//Write to a shared resource   For(IntI= 0; I< 10; I++){g_carray[10 -I- 1]=' B '; Sleep (1); }   //Leave the critical section G_cs. Unlock ();Return 0; } void Csampleview::oncriticalsectionmfc () {// start thread AfxBeginThread (ThreadProc1, NULL);  AfxBeginThread (THREADPROC2, NULL); // wait to finish calculation Sleep (300// report calculation results CString sresult = CString (G_carray); AfxMessageBox (Sresult); }

Transferred from: http://www.cnblogs.com/userinterface/archive/2005/04/27/146137.html

VC Multi-threaded Critical Zone (RPM)

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.