home/ atoms/ sonic-pi-in-thread

in_thread do ... end launches a concurrent execution path while the main thread continues

Wrapping a block in in_thread do … end starts it immediately on a new thread, allowing the main thread to continue to the next statement. Multiple in_thread blocks run concurrently, enabling layered beats, basslines and melodies. New threads inherit settings (use_synth, use_bpm, etc.) from their parent but changes inside do not propagate back. Each Run press is itself a thread, which is why pressing Run multiple times layers sounds.

Examples

in_thread do loop do sample :bd_haus sleep 1 end end

loop do use_synth :fm play 40, release: 0.2 sleep 0.5 end

Assessment

Build a three-layer patch (kick, bassline, melody) using in_thread. Explain why the last loop does not need in_thread.

“By wrapping the first loop in an `in_thread`”
corpus · sonic-pi-built-in-tutorial · chunk 12