0
static int initCount = 0;

int objInit(void)   
{ 

    int i;

    /* Check for reentrancy */

    TaskLock ();
    i = initCount++;
    TaskUnlock ();

    if (i > 0)
        {
        while (!initialized)
            TaskDelay (100);

        return (OK);
        }
    ......
}

Should the variable initCount be declared volatile?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zhang Baolei
  • 95
  • 1
  • 10

1 Answers1

2

No, it doesn't need to be volatile. However TaskLock () must impose some sort of memory barrier to ensure the operations are carried out in that order, i.e. first lock, then load initCount. It's likely your locking primitives already do this.


Volatile: Almost Useless for Multi-Threaded Programming.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • perhaps the initialized variable should be? Would this be optimized out? – Josh Petitt Feb 27 '13 at 04:38
  • 1
    @JoshPetitt Maybe if it's modified from a signal handler :-? Not sure though. If it's merely modified from another thread `volatile` isn't needed and at the same time not enough. – cnicutar Feb 27 '13 at 04:40