Timing/syncing issue

I’m experiencing an issue when communicating values from one live_loop to another:

set :cnt,0

live_loop :loop1, sync: :loop2 do
  use_real_time
  use_bpm 60
  sleep 0.1
  print "loop1", get[:cnt]
end


live_loop :loop2 do
  use_real_time
  use_bpm 60
  note, velocity = sync "/midi:midiin2_(lpminimk3_midi)_1:10/note_on"
  
  set :cnt, get[:cnt]+1
  
  print "loop2", get[:cnt]
end

I see the following log when I press a key and trigger a 2nd MIDI in event.

....
{run: 85, time: 3.248, thread: :live_loop_loop2}
 └─ "loop2" 2
 
{run: 85, time: 3.323, thread: :live_loop_loop1}
 └─ "loop1" 1
 
{run: 85, time: 3.423, thread: :live_loop_loop1}
 └─ "loop1" 1
 
{run: 85, time: 3.523, thread: :live_loop_loop1}
 └─ "loop1" 1
 
{run: 85, time: 3.623, thread: :live_loop_loop1}
 └─ "loop1" 2

How is it possible that loop1 needs 3 iterations before it sees the update of cnt?

It would be great if someone could run my program to confirm it is an Sonic Pi issue, and not somehow an issue in my System configuration. Thanks in advance!

I had a play with this this morning.
I think it works with the modification below


set :cnt,0

live_loop :loop1  do
  sync :loop2 #move the sync to here
  use_real_time
  use_bpm 60
  sleep 0.01 #makes sure set :cnt completed before you try and get it
  print "loop1", get[:cnt]
end


live_loop :loop2 do
  use_real_time
  use_bpm 60
  note, velocity = sync "/midi:usb_oxygen_8_v2:1/note_on" #adjusted for my keyboard
  
  set :cnt, get[:cnt]+1
  
  print "loop2", get[:cnt]
end

Also I think you could put the use_bmp outside the loops just once

Thanks for trying.

Loop1 will for example play a beat reacts on a shared variable that is set in loop2. So placing a “sync :loop2” into loop1 is problematic as it will wait for a new note.

I really don’t understand the behaviour. If loop1 has a sleep of 0.1 then it should lag at most one iteration.

I updated the code so that both threads start in sync

set :cnt,0

live_loop :loop1, sync: :main  do
  use_real_time
  use_bpm 60
  sleep 0.05 
  print "loop1", get[:cnt]
end


live_loop :loop2, sync: :main do
  use_real_time
  use_bpm 60
    
  note, velocity = sync "/midi:midiin2_(lpminimk3_midi)_1:1/note_on" #adjusted for my keyboard
  
  set :cnt, get[:cnt]+1
  
  print "loop2", get[:cnt]
end

sleep 1

cue :main # make sure both threads start synced

The log shows this:

At 11.8215 loop2 sets cnt to 2, but only at time 12.251 does loop1 see the change!

run: 14, time: 11.5892, thread: :live_loop_loop2}
└─ “loop2” 1

{run: 14, time: 11.651, thread: :live_loop_loop1}
└─ “loop1” 0

{run: 14, time: 11.701, thread: :live_loop_loop1}
└─ “loop1” 0

{run: 14, time: 11.751, thread: :live_loop_loop1}
└─ “loop1” 0

{run: 14, time: 11.801, thread: :live_loop_loop1}
└─ “loop1” 0

{run: 14, time: 11.8215, thread: :live_loop_loop2}
└─ “loop2” 2

{run: 14, time: 11.851, thread: :live_loop_loop1}
└─ “loop1” 0

{run: 14, time: 11.901, thread: :live_loop_loop1}
└─ “loop1” 0

{run: 14, time: 11.951, thread: :live_loop_loop1}
└─ “loop1” 1

{run: 14, time: 12.001, thread: :live_loop_loop1}
└─ “loop1” 1

{run: 14, time: 12.051, thread: :live_loop_loop1}
└─ “loop1” 1

{run: 14, time: 12.101, thread: :live_loop_loop1}
└─ “loop1” 1

{run: 14, time: 12.151, thread: :live_loop_loop1}
└─ “loop1” 1

{run: 14, time: 12.201, thread: :live_loop_loop1}
└─ “loop1” 1

{run: 14, time: 12.251, thread: :live_loop_loop1}
└─ “loop1” 2