home/ atoms/ chuck-shreds-sporking

ChucK shreds are sample-synchronous concurrent processes spawned with spork ~

A ChucKian process is called a shred; to spork a shred is to create and add a new process to the virtual machine (spork ~ myFunc();). ChucK offers sample-synchronous, non-preemptive concurrency: a shred only yields when it advances time, so multiple shreds — a drum loop, a melody, a modulator — can each be written as plain sequential code and co-scheduled at sample precision by the VM’s shreduler. Because control passes only at explicit time advances, there are no data races over shared UGen parameters. This lets independent musical layers run ‘in parallel’ with guaranteed timing coherence.

Examples

fun void kick(){ while(true){ /* kick */ 500::ms => now; } }
fun void snare(){ 250::ms => now; while(true){ /* snare */ 500::ms => now; } }
spork ~ kick();
spork ~ snare();
10::second => now;

Assessment

Predict the pattern from sporking a kick every 500 ms and a hat every 125 ms, then explain how non-preemptive scheduling prevents the two shreds computing the same sample simultaneously.

“A ChucKian process is called a shred. To spork a shred means creating and adding a new process to the virtual machine.”