How Thread Safe is a single <float> Used as a Gain Coefficient

Hi all,

I've been searching around and trying to get a firmer grasp on thread safe audio processing. In particular, I was wondering if a single <float> member is dangerous to use as a simple gain coeffecient where one thread is setting the value and another is reading it. I mean "safe" in that the value will never be read on the audio thread as some intermediate wacko value that could cause a sever blowup.

I think I'm asking if it's safe to assume that the assembly code for saving a float value from register to memory only takes one instruction. Here's a rough example:


struct Amp
{
    float gain;
};

Amp amp;

// Thread 1 can set the gain at any given time
void threadOneOp()
{
    amp.gain = 0.0f;
}

// Thread 2 can set the gain at any given time
void threadTwoOp()
{
    amp.gain = 1.0f;
}

// Thread 3 (audio thread) uses the gain value to process audio
// at any given time
void processBlock(float buffer*, int numSamples)
{
    for (int i = 0; i < numSamples; i++)
    {
        buffer[i] *= amp.gain;
    }
}

Will this work or should I consider using Atomic<float>?

Many thanks!

To be strictly correct and guaranteed that it'll work in all compilers and all architectures, then you should use Atomic or std::atomic for that. But in practical terms, yes, it will almost certainly work with current platforms and compilers.