[chuck-users] implementing a "wait for signal with timeout"

Robert Poor rdpoor at gmail.com
Mon Jun 8 13:53:57 EDT 2009


In real-time music making, sometimes you want to wait for a signal  
(e.g. event.broadcast()) OR for a specific time to elapse, whichever  
comes first.  I've implemented ways to do this, but I'm not really  
satisfied with the code.

Here's the problem: Lets say that your music is slaved to a metronome,  
and the metronome is allowed to change speed.  You want your music to  
stay sync'd to the metronome.  If you simply do:
	now + (1/tempo)::second => time next_beat;
	next_beat => now;
you'll be in trouble if the metronome speeds up while you're waiting:  
your note will be late.  The fundamental problem is that once you  
execute "next_beat => now;", you're committed to waiting and there's  
now way to break out of it, short of killing the thread.  The problem  
with killing the thread is that you leak 88K with each thread (!!!).

So here's the programming challenge: how would you implement a "wait  
for signal with timeout" that will block until it gets a signal OR a  
specified time has elapsed?  Since each thread costs 88K in non- 
reclaimed memory, you may create threads, but your solution must re- 
use them.

As I said, I have implementations which I'm happy to share, but I'd  
like to see how you would do this first.  Most of all, this is likely  
to turn into a feature request for the next version of ChucK.

- Rob

More information about the chuck-users mailing list