[chuck-users] Killing thread from without

Hans Aberg haberg at math.su.se
Tue Apr 21 15:13:04 EDT 2009

Is it possible to kill a thread by saving a reference and calling  
exit() (or something) on that reference? Are there any means by which  
a created thread can be manipulated from without it by its reference?

On a keyboard, I want a generator to be disconnected first some time  
after a key has been released - otherwise the sound will be truncated.  
How long depends on the generator, so I want to be able to set it  
arbitrarily. Then the problem is when the key is depressed again  
before this waiting time for disconnect has elapsed. One way to fix it  
is to let the thread check if the key has been depressed again, say by  
looking at the key down times, and if so, just skip the disconnect.

But in order to avoid threads hanging around, it would be more  
logically to kill off the old one when the key is depressed again, as  
it is not needed anymore. So then write this function:
   BeeThree s[13][5]; // Sound generator for key coordinates x, y.
   fun void schedule_note_disconnect(int x, int y, dur t) {
     t => now;
     s[x][y] =< r;
When a key with coordinates x, y is released, schedule a disconnect:
   Shred @ note_disconnect[13][5];
   1 => s[x][y].noteOff;
   spork ~ schedule_note_disconnect(x, y, 1100::ms) @=>  

Then, when a key is depressed, I want to cheer it up using:
   if (note_disconnect[x][y] != null)
   // Connect, set frequency and note on.

However, it is easy to see that this exit(), though syntactically  
legal, does not affect the thread at all. So in the example above, a  
long note following one (on the same key) shorter than 1100::ms will  
be cut off.


More information about the chuck-users mailing list