Threading nightmares
Re: Threading nightmares
Hmm, certainly food for thought. I'll think about that for a while.
But I'm increasingly thinking "just get on with it Colin" as it'll only take a day or two to code. If this didn't involve the loss of DAW automation (and meta programming where Adroit Custom can remote control itself) then it would be a no brainer. But then I weigh that against full control, live skinning and how much the stock slider's ridiculous jump annoys me. Plus I can achieve meta-programing via a CV control option using Custom IO.
The problem I can't see how to escape is that at some point I do actually need to call SetValueNoNotifcation() no matter where its called from as that's the only way to change the VoltageKnob or VoltageSlider's apppearance and how does this become thread-safe given that whatever mechanism I use it won't actually be thread-safe.
Most of the time as I keep saying things work but on occasion even though a knob is for instance visually at minimum setting it reports a value and tooltip of say 38%. How do I get to find out that the knob is actually at 0% if it thinks its at 38%?
But I'm increasingly thinking "just get on with it Colin" as it'll only take a day or two to code. If this didn't involve the loss of DAW automation (and meta programming where Adroit Custom can remote control itself) then it would be a no brainer. But then I weigh that against full control, live skinning and how much the stock slider's ridiculous jump annoys me. Plus I can achieve meta-programing via a CV control option using Custom IO.
The problem I can't see how to escape is that at some point I do actually need to call SetValueNoNotifcation() no matter where its called from as that's the only way to change the VoltageKnob or VoltageSlider's apppearance and how does this become thread-safe given that whatever mechanism I use it won't actually be thread-safe.
Most of the time as I keep saying things work but on occasion even though a knob is for instance visually at minimum setting it reports a value and tooltip of say 38%. How do I get to find out that the knob is actually at 0% if it thinks its at 38%?
Re: Threading nightmares
GetValue() appears to be just a one line wrapper that directly returns the component's value and I call it all the time in ProcessSample() without any problems. However SetValue() and SetValueNoNotification() certainly do need to be used with extreme caution.Waverley Instruments wrote: ↑Mon Jul 24, 2023 4:31 pm I also never do any VoltageComponent SetValue() or even GetValue() calls in ProcessSample(). Better safe that sorry!
Re: Threading nightmares
One other thing I tend to do in ProcessSample() is grab all the values I'm going to use (samples from input jacks, smoothed knob values etc) at the start of processing and build up a "frame" that represents a snapshot of state for the duration of that sample - the sources for all the values in that frame might be overwritten at random by other threads, but my frame remains invariant once settled.
I don't use GetValue() to read from knobs, but let Notify() push new targets into a smoothing function which I do read from. AFAIK there's no alternative to using GetValue() and SetValue() for getting samples out of input jacks and into output jacks? One area where the "try not to call native methods in ProcessSample" advice is a bit misleading...
I don't use GetValue() to read from knobs, but let Notify() push new targets into a smoothing function which I do read from. AFAIK there's no alternative to using GetValue() and SetValue() for getting samples out of input jacks and into output jacks? One area where the "try not to call native methods in ProcessSample" advice is a bit misleading...
- Waverley Instruments
- Posts: 147
- Joined: Thu May 05, 2022 2:10 pm
Re: Threading nightmares
SetValue() on VoltageAudioJack is marked as native (GetValue() is not...)
- Waverley Instruments
- Posts: 147
- Joined: Thu May 05, 2022 2:10 pm
Re: Threading nightmares
Ah... so it's a wrapper round some C++ / JUCE magic I guess.
Re: Threading nightmares
After pondering through this thread, I rechecked the VM Programmer's Guide and there is neither explanation nor guidance on threading. I looked at the examples in the SDK, and it didn't take long to see code that is definitely not thread safe itself. Is VM doing something to avoid certain problems? If so, there are still issues so either it's not fully working or certain coding patterns are problematic. If not, module developers need some guidance so we can handle it ourselves.
Neither ignoring threading issues, even if they rarely occur under most circumstances, nor having each developer invent (or re-invent) a solution to their specific issues seem like the best way to go. I opened a support ticket inviting a VM developer to this thread. Hopefully we'll be able to get some clarity on this.
Neither ignoring threading issues, even if they rarely occur under most circumstances, nor having each developer invent (or re-invent) a solution to their specific issues seem like the best way to go. I opened a support ticket inviting a VM developer to this thread. Hopefully we'll be able to get some clarity on this.
Re: Threading nightmares
Just a quick update.
It only took about an hour to write a rundimentary slider that safely handles being changed at sample rate from ProcessSample() and by mouse input simultaneously. It also doesn't do the ridiculous jump that the stock slider does.
The knob will be slightly trickier but I don't foresee any major problems.
It only took about an hour to write a rundimentary slider that safely handles being changed at sample rate from ProcessSample() and by mouse input simultaneously. It also doesn't do the ridiculous jump that the stock slider does.
The knob will be slightly trickier but I don't foresee any major problems.
Re: Threading nightmares
I realise that the posters here all have expert-level knowledge but many readers might be interested in learning more about the broader subject of concurrency in Java.
But if you look at the raw library documentation...
https://docs.oracle.com/javase/1.5.0/do ... mmary.html
https://docs.oracle.com/javase/1.5.0/do ... mmary.html
https://docs.oracle.com/javase/1.5.0/do ... mmary.html
...it seems pretty scary!
So here is the official overview...
https://docs.oracle.com/javase/tutorial ... index.html
And if you are in a hurry here is a useful essay about the different thread-safety approaches in Java...
https://www.baeldung.com/java-thread-safety
But if you look at the raw library documentation...
https://docs.oracle.com/javase/1.5.0/do ... mmary.html
https://docs.oracle.com/javase/1.5.0/do ... mmary.html
https://docs.oracle.com/javase/1.5.0/do ... mmary.html
...it seems pretty scary!
So here is the official overview...
https://docs.oracle.com/javase/tutorial ... index.html
And if you are in a hurry here is a useful essay about the different thread-safety approaches in Java...
https://www.baeldung.com/java-thread-safety
-
- Site Admin
- Posts: 23
- Joined: Mon Jan 03, 2022 9:13 pm
Re: Threading nightmares
It is possible for notifications to come from the GUI thread, timer threads, and even the sound engine thread if adjustments are made to knobs within the ProcessSample() function. However, it is not recommended to modify knobs within the ProcessSample() function as this is a time-sensitive thread and GUI functions shouldn't occur within it.