# Timing/syncing issue

**URL:** <https://in-thread.sonic-pi.net/t/timing-syncing-issue/7796>\
**Category:** Support, Help & Resources\
**Created:** [May 4, 2023, 9:22pm UTC](https://in-thread.sonic-pi.net/t/timing-syncing-issue/7796 "2023-05-04T21:22:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwjbosman1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@dwjbosman1](https://in-thread.sonic-pi.net/u/dwjbosman1)\
**Post date:** [May 4, 2023, 9:22pm UTC](https://in-thread.sonic-pi.net/t/timing-syncing-issue/7796/1 "2023-05-04T21:22:11Z")

</div>

I’m experiencing an issue when communicating values from one live\_loop to another:

```ruby
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.

```ruby
....
{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?

---

<div class="post-metadata">

**Author:** ![dwjbosman1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@dwjbosman1](https://in-thread.sonic-pi.net/u/dwjbosman1)\
**Post date:** [June 30, 2023, 7:04am UTC](https://in-thread.sonic-pi.net/t/timing-syncing-issue/7796/2 "2023-06-30T07:04:28Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [June 30, 2023, 1:04pm UTC](https://in-thread.sonic-pi.net/t/timing-syncing-issue/7796/3 "2023-06-30T13:04:35Z")

</div>

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

```ruby

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

---

<div class="post-metadata">

**Author:** ![dwjbosman1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@dwjbosman1](https://in-thread.sonic-pi.net/u/dwjbosman1)\
**Post date:** [June 30, 2023, 8:26pm UTC](https://in-thread.sonic-pi.net/t/timing-syncing-issue/7796/4 "2023-06-30T20:26:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dwjbosman1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@dwjbosman1](https://in-thread.sonic-pi.net/u/dwjbosman1)\
**Post date:** [July 1, 2023, 8:50am UTC](https://in-thread.sonic-pi.net/t/timing-syncing-issue/7796/5 "2023-07-01T08:50:09Z")

</div>

I updated the code so that both threads start in sync

```ruby
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
