A question of volatile

Source: Internet
Author: User

In embedded system, we usually use the keyword "volatile", what does the keyword mean?

Analysis
The main purpose of the volatile modifier is to prompt the compiler that the value of the object may be changed without the compiler being monitored, so the compiler cannot arbitrarily optimize the code for that object.

A simple compiler optimizer
1) Read the value of variable a into register X, as long as the X value is not changed, and later need to use the value of a to read from X
The role of volatile
1) Each time you need to use the value of a, you cannot read from X, you must read from a

The following description of volatile is available in the AVR Help documentation:
Declaring OBJECTS VOLATILE
There is three main reasons for declaring an object volatile:
Shared access; The object is gkfx between several tasks in a multitasking environment
Trigger access; As for a memory-mapped SFR where the fact of an access occurs have an effect
Modified access; Where the contents of the object can change in ways not known to the compiler.

Two examples of volatile variables
1) Variables shared by several tasks in multi-threaded applications
2) non-automatic variables that are accessed in an interrupt service subroutine

A question of volatile

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.