#2934·sonic-pi

Sonic pi non-deterministic behavior with multi-thread time state increment

Author: CharlesFaumanCreated Nov 13, 2021Updated Oct 20, 2024

While attempting to implement an atomic increment with sonic pi's time state system, I came across some non-deterministic behavior using the time state system. Below is the smallest reproduceable example I could come up with.

The following code is nondeterministic:

set :foo, 0

in_thread do
  set :foo, get[:foo] + 1
end

in_thread do
  set :foo, get[:foo] + 1
end

in_thread do
  set :foo, get[:foo] + 1
end

sleep 0.1
puts get[:foo]

it will print 1 most of the time, and about 1/10 of the time print 2. Note that adding more of the in_thread blocks increases the chance of a non-1 result.

System: I am running Sonic Pi v3.3.1 on Windows