[One day, one day] C language volatile

Source: Internet
Author: User

One C volatile per dayEvery day, I picked up a C-language shell, which grew up and formed thousands of miles.
Today's shells: C language volatile is used to avoid errors caused by Compiler optimization.The CPU accesses the memory much faster than the device address, and the access register speed is faster than the memory. To improve the program running speed, the compiler will optimize the program. For example:Buffer = Read (0xEE); // Read port 0xEE data and store it toA = buffer;Buffer=Read(0xEE );// Read port 0xEE data and store it to port B
B = buffer;The compiler may be optimized:Buffer=Read(0xEE );A=Buffer;B=Buffer; // This optimization can reduce one device access.Problem: if the content of port 0 x EE is updated in real time, the optimization will violate the program's original intention. volatile is used to solve this problem, prompting the compiler not to optimize the variable to ensure real-time performance. The usage is as follows:VolatileBuffer=Read(0xEE );Usage: volatile is used to modify variables that may be interrupted or modified by other threads.1. Program interruption2. multi-threaded ProgramVolatile is often used in program design.
Have a nice day!

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.