Mutual Exclusion protection is implemented without using system APIs.

Source: Internet
Author: User

Generally, the mutex protection of critical resources must be implemented using system APIs similar to take_mutex/give_mutex,

It usually needs to switch from the user space to the kernel space, and sometimes it may need to be interrupted. In order to achieve a low overhead,

To implement a simple mutex protection mechanism for critical resources, I have designed a method. I hope you can refer to it and give some suggestions.

Volatile int a = 0;

Volatile int B = 0;

/* Thread */

Void thread_A ()
{
While (1 ){
A ++;
If (B = 0 ){
Access_the_critical_resource;
A = 0;
Break;
} Else {
A = 0;
}
}
}

/* Thread B */

Void thread_ B ()
{
While (1 ){
B ++;
If (a = 0 ){
Access_the_critical_resource;
B = 0;
Break;
} Else {
B = 0;
}
}
}

Let's analyze why the two threads can achieve mutual exclusion,

Thread A first adds thread A to 1, and then queries the variable B. If thread B is 0, it indicates that thread B has not added thread B to 1, or thread B has added thread B to 1, but it has not been written back to the memory. In short, the line B is still running at a location before the B ++ statement, or the operation B = 0 (that is, a location between B = 0 and B ++) is just executed, So we conclude that B is not in the critical section at this time, therefore, thread A can perform access to the next critical zone.

If, when thread A checks the variable B, if the variable B is not equal to 0, then thread B may be accessing the critical section, or it may only have executed the B ++ statement, but it has not entered the critical section. At this time, thread A avoids the risk of entering the critical section with thread B through the if (B = 0) judgment.

Further analysis, because thread A adds 1 to its switch variable abefore trying to enter the critical section, once the mutex check is passed (if (B = 0 )), it means you can enter the critical section with peace of mind, because at this time you can be sure that the other side is running in B = 0;

And B ++; between these two statements, rather than between B ++ and B = 0 (please note the order of these two statements), so when A enters the critical section, during this period of time in the critical section, B will be blocked outside the critical section by the if (a = 0) condition until A has a critical section and runs a = 0; break ;,

In this case, B may enter the critical section.

Based on symmetry, the Analysis of A and B is the same. Of course, this method has many disadvantages. In extreme cases, two threads may not be able to enter

I will not list them here. I just want to make a trial.

In the SMP environment, this method should also be acceptable without logical errors.

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.