Critical section usage

Source: Internet
Author: User
Critical Section

Critical section:
Whether it is a hardware critical resource or a software critical resource, multiple processes must access it with mutual exclusion. The code used to access critical resources in each process is called the critical section ).
The program that accesses critical resources in each process is called a critical section (a critical resource is a shared resource that can only be used by one process at a time ). Only one process can enter the critical section at a time. Other processes are not allowed to enter the critical section. Whether it is a hardware critical resource or a software critical resource, multiple processes must access it with mutual exclusion.
Critical zones involving the same critical resource in multiple processes are called related critical zones.
The scheduling principle for a process to enter the critical section is as follows: ① if several processes require to enter the idle critical section, only one process can be entered at a time. ② At any time, there must be no more than one process in the critical section. If a process has entered its own critical section, all other processes that attempt to enter the critical section must wait. ③ Processes entering the critical section should exit within a limited period of time so that other processes can enter their critical section in time. ④ If a process cannot enter its critical section, the CPU should be used out to avoid the process being "busy.
If multiple threads attempt to access the critical section at the same time, all other threads attempting to access the critical section will be suspended and will continue until the thread enters the critical section. After the critical section is released, other threads can continue to seize it and use the atomic method to share resources.
When a critical section is used, the shared resources are protected by the critical_section structure object. The entercriticalsection () and leavecriticalsection () functions are used to identify and release a critical section respectively. The critical_section structure object used must be initialized by initializecriticalsection () before it can be used. It must ensure that any code in all threads attempting to access this shared resource is under the protection of this critical section. Otherwise, the critical section will not play its due role, and shared resources may still be damaged.
The following code demonstrates the role of the critical section in protecting resources shared by multiple threads. Use two threads to write the global variable g_carray [10] separately. Use the g_cs in the critical section to synchronize the thread and initialize it before enabling the thread. To make the experiment more effective and reflect the function of the critical section, when the thread function writes to the shared resource g_carray [10], the delay of the sleep () function is 1 millisecond, increase the possibility of other threads occupying CPU. If you do not use the critical section to protect it, the shared resource data will be damaged (see figure 1 (), the correct results can be obtained after the thread is synchronized using the critical section (see Figure 1 (B ). The code implementation list is attached:
// Structure object of the critical section
Critical_section g_cs;
// Share resources
Char g_carray [10];
Uint threadproc10 (lpvoid pparam)
{
// Enter the critical section
Entercriticalsection (& g_cs );
// Write shared resources
For (INT I = 0; I <10; I ++)
{
G_carray =;
Sleep (1 );
}
// Exit the critical section
Leavecriticalsection (& g_cs );
Return 0;
}
Uint threadproc11 (lpvoid pparam)
{
// Enter the critical section
Entercriticalsection (& g_cs );
// Write shared resources
For (INT I = 0; I <10; I ++)
{
G_carray [10-I-1] = B;
Sleep (1 );
}
// Exit the critical section
Leavecriticalsection (& g_cs );
Return 0;
}
......
Void csample08view: oncriticalsection ()
{
// Initialize the critical section
Initializecriticalsection (& g_cs );
// Start the thread
Afxbeginthread (threadproc10, null );
Afxbeginthread (threadproc11, null );
// Wait until computation is completed
Sleep (300 );
// Report the calculation result
Cstring sresult = cstring (g_carray );
Afxmessagebox (sresult );
}
When a critical section is used, it is generally not allowed to run for a long time. As long as the thread entering the critical section has not left, all other threads attempting to enter the critical section will be suspended and enter the waiting state, and will affect the running performance of the program to a certain extent. In particular, do not include operations waiting for user input or other external interventions in the critical section. If you enter the critical section but have not been released, it will also cause other threads to wait for a long time. In other words, no matter what happens after the entercriticalsection () Statement enters the critical section, make sure that the matching leavecriticalsection () can be executed. You can add a structured exception handling code to ensure the execution of the leavecriticalsection () statement. Although the synchronization speed in the critical section is very fast, it can only be used to synchronize threads in the current process, but not to synchronize threads in multiple processes.

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.