Whether global variables (integer type) need to be synchronized in multiple threads

Source: Internet
Author: User

Background: if there is a global variable A of the int type, thread a only performs read operations on a, and thread B only performs write operations on, so do the two threads need to lock during operation a to maintain synchronization?

This cannot be determined simply by locking or not locking. It should be discussed in detail.

Scenario 1: If thread a reads a only for display to the interface, or a executes some operations after certain conditions are met, during these operations, A is not concerned about whether a has changed. after a certain period of time, the above operations are also performed. In this case, no locks are required.

The reason is that when thread a reads data from a, it reads data completely. Similarly, thread B writes data to a. There is no problem of reading or writing half of. There may be new questions. This doesn't mean that the operation on a is atomic, so why do we need the type of atomic variables, in addition, the operation on a is not completed by a command. In fact, this question is not in conflict with the saying that "there is no read or write half of. Because "there is no read or write half of a", it does not mean that the operation on a is an atomic operation. For example, if a is a + 1, this operation is definitely not an atomic operation. This statement is easy to convert to a machine-like language. The first step is to read a to a register, and the second step is to add 1 to the value in the register, step 3: write the new value in the Register to. Since three steps are required, it is obviously not an atomic operation. So what does "there is no read or write half of a" mean? It means that the second step is a successful execution. In other words, thread B needs to write a, and completes any step in three steps. Thread a Reads A and A is still a valid value, except that A is not necessarily the latest value. In Case 1, thread A does not care about whether a is the latest value. It only cares about whether a is a valid value written by thread B. As for the latest value, it can be refreshed continuously through loops. Some people may wonder why the second step is a successful execution? Is it rare that the CPU will not be snatched by other threads when half of the execution is performed? No, because there is byte alignment, only one bus cycle is required for a read or write cycle. In this bus cycle, this integer variable is processed, the CPU will not be preemptible before the end of a bus cycle, that is, the interruption will not lead to the CPU being preemptible when half of the execution of a bus cycle (the CPU responds to the interruption after the current command ends, that is, when the last t state in the last bus cycle in the last command cycle is run, the CPU samples the intr line to check whether there are interrupt requests ).

 

Case 2: thread a only performs read operations on a, and then performs corresponding operations on, if the write operation of thread B on a changes the operation, the lock is required.

 

the purpose of locking is to protect data. If the data itself can be consistent without protection, there is no need to lock the data :).

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.