What about multithreading (atomic 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]

Atomic locks are a feature of multi-threaded programming. However, in normal software writing, there are not many atomic locks. There are many reasons for this. I think there are two main reasons. First, there are few introductions on atomic locks. Second, people are used to the existing solutions in programming. If there is no special requirement, they can just rashly modify the existing code. After all, for many people, do not seek merit, but do not. It is important to maintain the stability of the current Code.
In fact, as early as the blog "multi-threaded data mutex", we have already introduced atomic locks. This blog mainly discusses how to use atomic locks. Some of the usage in the middle is just my personal experience. I hope to hear more about your ideas.

(1) Searching for atomic locks in a function 

In some functions, we sometimes need to find data that meets certain features. In the traditional single-core CPU, the optimized space is relatively limited. However, the multi-core CPU has become the mainstream configuration. Therefore, we can divide the search work into several subfunctions and perform parallel operations on several cores. However, this involves a problem, that is, access to public data. The traditional access method should be like this,

unsigned int count = 0;int find_data_process(){    if(/* data meets our standards */){         EnterCriticalSection(&cs);         count ++;         LeaveCriticalSection(&cs);             }}

We can see that the lock is used in the code, so it will inevitably involve system calls and function scheduling. Therefore, the execution efficiency is greatly reduced. What if we use an atomic lock?

unsigned int count = 0;int find_data_process(){    if(/* data meets our standards */){        InterLockedIncrement(&count);    }}

If you are interested, you can make such a question ~ How many digits can be divisible by 3 on 0xffffffff? You can also verify the code efficiency after replacing the critical section with an atomic lock. For multi-core and multi-thread programming, you can refer to the blog "multi-thread basics.

(2) Atomic locks in code segments
The preceding example only describes atomic locks in the statistical function. So how can we replace traditional system locks with atomic locks? For example, if the original data access is like this,

void data_process(){    EnterCriticalSection(&cs);    do_something();    LeaveCriticalSection(&cs);   }

What will happen if I change it to an atomic lock?

unsigned int lock = 0;void data_process(){    while(1 == InterLockedCompareExchange(&lock, 1, 0));    do_something();    lock = 0;    }

Here, atomic locks are used to replace common system locks, and the completed functions are actually the same. So what are the differences between them? In fact, the key is how long do_something will be executed. For example, we are going to buy steamed buns, but there are many people who buy them. What should we do? There are two options: if the person who sells steamed stuffed bun is lucky enough to serve a customer in 10 seconds, even if there are 50 people in the queue, we only need to wait 7 or 8 minutes, this is worth the time. But if it takes one minute for the boss who sells steamed stuffed bun to serve a customer, it would be a sad reminder. If there were 50 people in front of the customer, we will be waiting for more than 50 minutes. 50 minutes is not a short time for us. We can use this time to buy fruit and pay for water and electricity. It is not too late to buy steamed stuffed buns after this time.

Like in the above example, the busy wait method is atomic lock, and the method that comes later is the traditional system lock. The do_something time value is not worth waiting.

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.