开发者

What is the effect of InterlockedIncrement argument declared as volatile

开发者 https://www.devze.com 2023-01-15 21:39 出处:网络
InterlockedIncrement and other Interlocked operations declare their arguments as volatile. Why? What is the intent开发者_如何学Pythonion and effect of this?The probable effect is very minimal. The mos

InterlockedIncrement and other Interlocked operations declare their arguments as volatile. Why? What is the intent开发者_如何学Pythonion and effect of this?


The probable effect is very minimal. The most likely intent is to allow users to pass volatile-qualified variables to these functions without the need for a typecast.


This is done so that the function can be called both on normal variables and on volatile variables. You cannot pass a volatile variable into a function which is not expecting a volatile argument. The following code does not compile (tested with Visual Studio 2005 C++ compiler):

void TestV(int *a)
{
  *a = 1;
}

void Test()
{
  volatile int a = 0;
  TestV(&a);
}

With the declaration being what it is, you can do following:

volatile LONG a = 0;

InterlockedIncrement(&a);

As it most likely has a sense to call InterlockedIncrement on volatile variables, it seems sensible to have it declared as such.


The volatile argument prevents the compiler from making optimizations that prevent the read to memory. A compiler can make whatever optimizations it likes as long as the visible effects of the code are the same, but those effects aren't considered in multiple thread scenarios, only from a single thread. The volatile keyword tells the compiler that this variable may be modified or read from external, unknowable sources and the compiler cannot get rid of it or ellide memory accesses to it.


In short, volatile means "this variable might be modified outside this program".

In a bit more words, it means that a variable's value can change without any related instructions in your code. It's often used in low-level I/O for instance, where the value of a register or buffer can be set by the hardware.

0

精彩评论

暂无评论...
验证码 换一张
取 消