What about multithreading (C ++ lock)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Programming is not easy, but it is not easy to write multi-threaded programs. I believe that a program that has compiled multiple threads should have such a painful process. What is the situation? Friends should have a look at the code to understand,

void data_process(){    EnterCriticalSection();      if(/* error happens */)    {        LeaveCriticalSection();        return;    }    if(/* other error happens */)    {        return;    }    LeaveCriticalSection();}

The above code illustrates a situation. This multi-thread mutex situation is often encountered in the coding process. Therefore, each operation on shared data requires entercriticalsection and leavecriticalsection. However, this is not smooth sailing. You may encounter various errors. At this time, your program needs to jump out. You may need to exit the critical section when an error occurs at the beginning. However, if there are more errors, you may not remember this operation. This error is complete, and other threads have no chance to obtain the lock.
Is it possible to use the C ++ feature to automatically handle this situation? Yes. Let's look at the following code,

class CLock{    CRITICAL_SECTION& cs;public:    CLock(CRITICAL_SECTION& lock):cs(lock){        EnterCriticalSection(&cs);    }    ~CLock() {        LeaveCriticalSection(&cs);    }}class Process{    CRITICAL_SECTION cs;    /* other data */public:    Process(){        InitializeCriticalSection(&cs);    }    ~Process() {DeleteCriticalSection(&cs);}    void data_process(){        CLock lock(cs);        if(/* error happens */){            return;        }        return;    }}

An important feature of C ++ is that the system automatically calls class destructor no matter when the function exits. In the data_process function of the Process class, the function creates a clock class at the beginning. When this class is created, the primary key of the critical section is started. So once it enters the critical section, can the critical section be exited in time in error? At this time, the advantages of C ++ destructor have emerged. No matter when an error occurs, the system will help us after the function exits. What should we do? That is, the system will call the clock destructor, that is, exit the critical section. In this way, our goal is achieved.
In fact, this is a C ++ trick.

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.