home/ atoms/ sonic-pi-time-state

Sonic Pi's Time State (get/set) provides deterministic shared state across live_loops

The Time State is a global key-value store accessed with set :key, value and get[:key]. Unlike plain variables, Time State accesses are sequenced by logical time, preventing race conditions. The same run always produces the same result regardless of thread scheduling order. set and cue share the same underlying mechanism; sync waits for future Time State insertions. Values should be immutable: numbers, symbols, rings, or frozen strings.

Examples

live_loop :setter do set :note, (scale :e3, :minor_pentatonic).tick sleep 0.5 end

live_loop :player do play get[:note] sleep 0.5 end

Assessment

Replace shared variable code with get/set and verify the log output is always sorted. Explain why the Time State version is deterministic.

“sharing information across live loops and threads, use `get` and `set` instead of variables for deterministic, reproducible behavi”
corpus · sonic-pi-built-in-tutorial · chunk 21