"" Volatile

Source: Internet
Author: User

From: http://www.avrw.com/article/art_101_3040.htm

 

What is the definition of the keyword volatile?

Volatile indicates that the variable is "changeable ". If a variable is changed by another reference or in another parallel task (for example, the service program is interrupted), it must be explicitly described as "volatile ", otherwise, an incorrect judgment will be made during the Compiler Optimization phase. For example, after the variable is read into the register, the value in the register will be used until the value is assigned to the variable, in fact, the value of this variable may have been changed by a pointer reference, or changed in the interrupted service program. The following example illustrates this error:

There is a variable T, which is reduced by one every fixed time in the scheduled interrupt, and then waited in the main program for it to be reduced to 0.

Unsigned char T;

Void t0_int (void) interrupt 1
{
...
T --;
...
}

Void main (void)
{
...
T = 10;
While (T! = 0);/* This will become an endless loop in Some compilers, rather than waiting for a period of time */
...
}

The correct statement should be to change the first sentence:

Volatile unsigned char T;

 

Bytes ------------------------------------------------------------------------------------------------------------------------------

 

From: http://www.cnblogs.com/tracy_hh/archive/2009/02/11/1388427.html

 

The volatile keyword indicates that a field can be modified by multiple concurrent threads. Fields declared as volatile are not restricted by Compiler Optimization (assuming that they are accessed by a single thread. This ensures that the field displays the latest value at any time.

The volatile modifier is usually used for fields accessed by multiple threads but not serialized by using the lock statement.

The volatile keyword can be applied to the following fields:

  • Reference type.

  • Pointer type (in an insecure context ). Note that although the pointer itself can be variable, the object it points to cannot be variable. In other words, you cannot declare a pointer to a mutable object ".

  • Integer, such as sbyte, byte, short, ushort, Int, uint, Char, float, and bool.

  • An Enumeration type with an integer base type.

  • It is known that it is a generic parameter of the reference type.

  • Intptr and uintptr.

Variable keywords can only be applied to class or structure fields. Local variables cannot be declared as volatile.

The following example shows how to declare a public field variable as volatile.

// csharp_volatile.cs
// Compile with: /target:library
class Test
{
   public volatile int i;
 
   Test(int _i)
   {
      i = _i;
   }

}

 

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.