Quantcast
Viewing latest article 32
Browse Latest Browse All 45

embedded C - using "volatile" to assert consistency

Consider the following code:

// In the interrupt handler file:volatile uint32_t gSampleIndex = 0; // declared 'extern'void HandleSomeIrq(){    gSampleIndex++;}// In some other filevoid Process(){    uint32_t localSampleIndex = gSampleIndex;   // will this be optimized away?    PrevSample      = RawSamples[(localSampleIndex + 0) % NUM_RAW_SAMPLE_BUFFERS];    CurrentSample   = RawSamples[(localSampleIndex + 1) % NUM_RAW_SAMPLE_BUFFERS];    NextSample      = RawSamples[(localSampleIndex + 2) % NUM_RAW_SAMPLE_BUFFERS];}

My intention is that PrevSample, CurrentSample and NextSample are consistent, even if gSampleIndex is updated during the call to Process().

Will the assignment to the localSampleIndex do the trick, or is there any chance it will be optimized away even though gSampleIndex is volatile?


Viewing latest article 32
Browse Latest Browse All 45

Trending Articles