Volatile in C + +

Source: Internet
Author: User

Volatile only guarantees its "visibility" and does not guarantee its "atomicity".

Execute count++; This statement consists of 3 instructions:

(1) Load the value of count from memory to a register r of the CPU;

(2) The value of the Register R +1, the result is stored in the register s;

(3) writes the value in the register s back to memory.

Therefore, if there are multiple threads executing count++ at the same time, the other thread cannot see the result of its execution until a thread executes step (3). (There are doubts: threads execute count++ at the same time, in order to ensure its atomicity, why not add mutex lock?) But to seek volatile?)

In the absence of volatile, after executing the count++, the execution result is actually written to the CPU cache, not immediately write back to memory, in some cases (such as insufficient CPU cache) and then flush the values in the CPU cache to memory. Because it is not stored in memory, other threads cannot see the results of the execution in time.

When there is volatile, the execution of the count++ is performed, the results are written to the cache and written in memory, so that other threads can see the results of the execution immediately.

However, volatile does not guarantee atomicity, and when a thread executes (1) (2) (3), volatile does not lock the value of count, that is, it does not block other threads from executing (1) (2) (3). There may be two threads executing at the same time (1), so (2) The same result is computed, and then (3) The same value is stored back. Consider the following section of code:
1  - ; 2 3  while  - )4{5    //Your code6 }

At this point, the compiler optimizes the code to:

1 // Your code

Because the compiler thinks that Some_int has not been changed, it has been 100. However, in multi-threading, if the first line is executed, but the third row is not executed, the other thread modifies the some_int,while and cannot enter the loop. When volatile is added, the compiler is prevented from optimizing, and each read of the Some_int is read from memory, not the storage of this thread (which, of course, loses efficiency). This is the effect of volatile.

Bottom line: Volatile guarantees that the thread can read the latest data because it is read from memory and stored in memory.  Rather than read and write in the respective registers of the thread. Question: I know how volatile works, but why make sure it's "visibility"? There is always a requirement that a thread might be monitoring a variable that can be called by many threads. I understand, right? If this is the case, the multi-threaded with static can, only one copy of the value, is the register also optimized? A: Since static is the only thing in the world, it should be shared between threads, so a thread changes it to other threads to see it, but this is not true, in order to improve the optimization and run speed, a thread modification of this variable is not necessarily immediately write back to the memory for other threads to see, At this point, you need to use volatile to force each change to write back to memory.

Volatile in C + +

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.