Is juce::SmoothedValue thread safe?

Hi,

Looking into smoothed value for some parameter interpolation, but it doesn’t seem like on first glance SmoothedValue is thread safe.

It seems like looking at the code that it would be totally possible for setTargetValue and getNextValue to be called at the same time from different threads.

This would create a race condition on updating the step member variable, which is updated in setTargetValue, and used in getNextValue.

I don’t see any atomics, or any locking to synchronize things. I want to be able to safely call setTargetValue from a UI thread, and getNextValue from the audio thread.

Am I missing something obvious?

Thanks for the help!

SmoothedValue is not thread safe, and it’s a good thing it’s not, since atomics are barriers for the optimizer.

Instead, you can create your own atomic, write into it in the UI thread, and set the value on the smoother in the audio thread. Alternatively you can use a parameter (AudioParameterInt/Float/etc) that are already atomic internally.