Multi-threaded interview second kill series 6--critical section function usage

Source: Internet
Author: User

last time we gave a small program, this time we use the critical section to solve the problem of the last one, the critical area is actually only four functions, namely, initialization, destruction, entering the critical section, leaving the critical area.

function function: Initialize
Function Prototypes:
void InitializeCriticalSection (lpcritical_sectionlpcriticalsection);
Function Description: The key segment variable must be initialized before it is defined.

function function: Destroy
Function Prototypes:
void DeleteCriticalSection (lpcritical_sectionlpcriticalsection);
Function Description: After use, remember to destroy.

function function: Enter key area
Function Prototypes:
void EnterCriticalSection (lpcritical_sectionlpcriticalsection);
Function Description: The system ensures that each thread is mutually exclusive into the critical area.

function function: Off-switch key area
Function Prototypes:
void LeaveCriticalSection (lpcritical_sectionlpcriticalsection);
Function Description: Exits the critical section, allowing other threads to enter.

In this program I have two variables we need to control, one is the sequence number of the main program and the global variables in the thread. Now we want to use the critical section to make the global variables in the sub-thread mutually exclusive, the critical section can be used for mutual exclusion between threads, but not thread synchronization , for what reason we will explain below. Here we look at the program, the program is about to create 10 threads, the thread inside the critical section of the control. Write the program today also encountered a small problem is prompted me ' _beginthreadex ': undeclared identifier, in fact, the reason is that the project should be set to multi-threading, in the settings of--c/c++--codegeneration Select Multi-Threading.

#include <stdio.h> #include <process.h> #include <windows.h>long g_nnum;unsigned int __stdcall Fun ( void *ppm); const int thread_num = 10; critical_section G_csthreadparameter,g_csthreadcode;int Main () {printf ("jd--critical section \ n"); InitializeCriticalSection ( &g_csthreadparameter); initializecriticalsection (&g_csthreadcode); HANDLE Handle[thread_num];g_nnum = 0;int i = 0;while (i<thread_num) {entercriticalsection (&g_csthreadparameter) ; Handle[i] = (handle) _beginthreadex (null,0,fun,&i,0,null); ++i;} WaitForMultipleObjects (thread_num,handle,true,infinite); return 0;} unsigned int __stdcall fun (void *ppm) {int nthreadnum = * (int*) PPM; LeaveCriticalSection (&g_csthreadparameter); Sleep (50); EnterCriticalSection (&g_csthreadcode);//Enter the critical section, at which time only one thread can access the g_nnum++; Sleep;p rintf ("Thread number%d global resource value is%d\n", nthreadnum,g_nnum); LeaveCriticalSection (&g_csthreadcode);//Leave the critical section return 0;}

We can see in the code that the main thread of the variable I also control, but the result is not satisfactory, the above we talked about the critical section can not be synchronized between threads, because of the concept of thread ownership in the critical section, what does this mean, for example, we enter the critical section in the main thread, Equivalent to the main thread to get the critical section of the ownership can be multiple entry, not the role of the critical section, that is, can be called multiple times enter, the normal critical area of the meaning is to enter the critical area after changing the variable after the change, but now the situation is to enter the critical section after the change of variables, However, the critical section is not highlighted, and then it is possible to enter the critical section again, since the critical section is introduced in the sub-thread, and the order of possible execution is somewhat different from what you think.

In fact, the simplest way you can look at the phenomenon is that you can add breakpoints to debug the program to know the order of execution of the program.

Multi-threaded interview second kill series 6--critical section function usage

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: 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.