Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the effect of InterlockedIncrement argument declared as volatile

InterlockedIncrement and other Interlocked operations declare their arguments as volatile. Why? What is the intention and effect of this?

like image 867
Suma Avatar asked Dec 28 '22 07:12

Suma


2 Answers

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.

like image 198
Bart van Ingen Schenau Avatar answered Jan 29 '23 18:01

Bart van Ingen Schenau


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.

like image 25
Suma Avatar answered Jan 29 '23 17:01

Suma