home/ atoms/ sonic-pi-named-threads

Named threads in Sonic Pi prevent duplicate instances when Run is pressed multiple times

Passing name: :symbol to in_thread ensures only one thread of that name runs at a time. If Run is pressed while a named thread is already running, Sonic Pi skips creating a new one. Unnamed threads always spawn a new instance on every Run press, causing layering. Named threads are the bridge between in_thread and live_loop; live_loop extends this with hot-swap semantics where the new code definition is picked up at the next iteration.

Examples

in_thread(name: :bass) do loop do sample :bd_haus sleep 0.5 end end

Assessment

Press Run three times on an unnamed loop vs a named loop. Describe the audible difference. Explain when you would prefer named in_thread over live_loop.

“only one thread of a given name may be running at the same time”
corpus · sonic-pi-built-in-tutorial · chunk 12