Hey Community,
So for the past couple of weeks I have been doing research on the topic. I have read couple of similar disscusion and the subject still baffles me, so I thought I would throw the question here and see if somebody has an insight.
for simplicity lets say I am doing the following in processBlock:
dynamicObject->ProcessingStereo(buffer.getReadPointer(0), buffer.getReadPointer(1), buffer.getWritePointer (0), buffer.getWritePointer (1), buffer.getNumSamples());
now lets say we want to reallocate dynamicObject, ok we cant do it on audio thread (real-time audio waits for nothing, new and delete are actually blocking instructions and all that), so how would you ensure thread-safety when allocating dynamicObject on GUI thread (or any other thread)?
my Idea is to call this function from GUI, and define a bool flag letting GUI-thread know that something has changed in audio-thread:
in processor I define:
void reloadObject() {
delete dynamicObject;
dynamicObject = new DynamicObjectType;
}
void checkIfSettingsChanged() {
// if something has changed set
settingsChanged = true
}
and back in processBlock, I modify as follows:
checkIfSettingsChanged();
if (settingsChanged) {// just deliver zeros, when we are reallocating stuff
for (int channel = 0; channel < getTotalNumInputChannels(); ++channel)
{
float* channelData = buffer.getWritePointer (channel);
for (int b = 0; b < buffer.getNumSamples(); ++b) {
channelData[b] = 0;
}
}
}
} else {
dynamicObject->ProcessingStereo(buffer.getReadPointer(0), buffer.getReadPointer(1), buffer.getWritePointer (0), buffer.getWritePointer (1), buffer.getNumSamples());
}
meanwhile in GUI thread ( or Editor) we have a timerCallback that does the following:
void timerCallBack() {
if (settingsChanged) {
getProcessor().reloadObject();
settingsChanged = false;
}
}
taking into consideration that I am defining settingsChanged as atomic, is this a safe design? or am I going to have issues with memory synchronization or tearing of any kind.