Error selecting program in block

Could be that you can’t initialize the Condition with true and instead set test to true in the action function. TBH, I always forget how Condition works…

Personally I tend to use CondVar because the semantics are more clear to me. Just don’t forget the boolean check in the call to wait!

I have tried to do away with the boolean check in wait as I don’t understand why it is needed - but it definitely seems to be necessary - can you explain why that is?

So why is it not sufficient to have the action signal the condvar which should wake up the thread sitting on wait (without a predicate) which can then check if the plugin is open. But when I try it like that it seems wait block forever - but why is that?

This seems to work:

(
fork {
    var cond = Condition.new();
	SynthDef(\mysdef, { |out = 0| Out.ar(0, VSTPlugin.ar(numOut: 2)) }).add;
	s.sync;

	~pt = VSTPluginController(Synth(\mysdef)).open("Pianoteq 8.so", action: { cond.test = true; cond.signal() });
    cond.wait;
    if (~pt.isOpen) {
		"open".postln;
		~pt.program_(26);
    } {
        "NOT open".postln;
   }
}
)

You are talking about CondVar and not Condition, right? Classic condition variables always need a predicate because they do not ‘know’ that they have been signalled. If the async operation is very fast, the callback may be scheduled before the main routine calls wait. In this case, signalOne has no effect because the routine isn’t waiting yet. Without the predicate, the subsequent wait call would block forever because nobody will signal it. With the predicate, it will just return immediately without waiting.

Yeah, that’s how Condition is supposed to be used. I got a bit confused there.

With CondVar, you can dispense with the variable I believe:

(
fork {
    var cv = CondVar();
	
	~synth = Synth(\vst);
	
    ~fx = VSTPluginController(~synth).open("foo", action: { cv.signalOne });
	
	// also, I'll add a timeout
	// so that failure doesn't block the thread forever
	cv.waitFor(5) { ~fx.isOpen };

	if (~fx.isOpen) {
        // now you can use the plugin
		~fx.editor;
		~fx.loadPreset("baz");
    } {
        // error handling
    }
}
)

And CondVar has a timeout feature, which allows you to continue in the thread in case of failure.

The waitFor timeout is a good reason to prefer CondVar over Condition.

hjh

FWIW, that’s what I originally had in Error selecting program in block - #11 by Spacechild1 before @flower correctly pointed out the missing error handling.

The 5 second timeout is a neat compromise.

I think, though, that without a timeout, it’s impossible to advance beyond the wait-with-predicate e.g. cv.wait { done } until the predicate is true…? So the error handling branch would never be reached.

CondVar can:

  • wait without a predicate – always advances when signaled, even if the async op was unsuccessful;
  • wait with a predicate, and no timeout – no way past the barrier except success (failure means the thread blocks forever);
  • wait with a predicate and timeout – the thread can advance right away on success, or after the timeout on failure.

hjh

I think, though, that without a timeout, it’s impossible to advance beyond the wait-with-predicate e.g. cv.wait { done } until the predicate is true…?

In my example in Error selecting program in block - #16 by Spacechild1 done will always be set by the action function and wait will eventually return (assuming that OSC messages are not dropped).

As I explained in Error selecting program in block - #24 by Spacechild1, CondVar always requires a predicate. The predicate can be external, though:

while { done.not } { cv.wait };

is equivalent to

c.wait { done };

2 posts were split to a new topic: Async ops (again)

That indeed looks quite elegant.

Is the lack of a “proper async programming model” a general point of pain when you do larger projects?

I don’t understand a word but you seem to know your stuff…