Translate

a = (ring 6, 5, 4, 3, 2, 1)
live_loop :shuffled do
a = a.shuffle
sleep 0.5
end
live_loop :sorted do
a = a.sort
sleep 0.5
puts "sorted: ", a
end
SourceTranslationState
638
Now we have caught and remembered `s` as a variable, which allows us to control the synth as it is running:
Ora che abbiamo catturato `s` come variabile, possiamo controllare il sintetizzatore mentre sta funzionando:
639
s = play 50, release: 8
sleep 2
control s, note: 62
s = play 50, release: 8
sleep 2
control s, note: 62
640
We'll look into controlling synths in more detail in a later section.
Vedremo come controllare i sintetizzatori in modo più dettagliato più tardi in questa sezione.
641
Warning: Variables and Threads
Attenzone: Variabili e Threads
642
Whilst variables are great for giving things names and capturing the results of things, it is important to know that they should typically only be used locally within a thread. For example, *don't do this*:
Mentre le variabili sono ottime per assegnare nomi alle cose ed acquisire i relativi risultati, è importante sapere che dovrebbero essere generalmente usate in locale all'interno di un thread. Per esempio, *non fare questo*:
643
a = (ring 6, 5, 4, 3, 2, 1)
live_loop :shuffled do
a = a.shuffle
sleep 0.5
end
live_loop :sorted do
a = a.sort
sleep 0.5
puts "sorted: ", a
end
644
In the above example we assign a ring of numbers to a variable `a` and then used it within two separate `live_loop`s. In the first live loop every `0.5`s we sort the ring (to `(ring 1, 2, 3, 4, 5, 6)`) and then print it out to the log. If you run the code, you'll find that the printed list *is not always sorted!*. This may surprise you - especially that sometimes the list is printed as sorted, and sometimes it is not. This is called non-deterministic behaviour and is the result of a rather nasty problem called a race-condition. The problem is due to the fact that the second live loop is also manipulating the list (in this case shuffling it) and by the time the list is printed, sometimes it has just been sorted and sometimes it has just been shuffled. Both live loops are racing to do something different to the same variable and every time round a different loop 'wins'.
645
There are two solutions to this. Firstly, *don't use the same variable in multiple live loops or threads*. For example, the following code will always print a sorted list as each live loop has its own separate variable:
646
live_loop :shuffled do
a = (ring 6, 5, 4, 3, 2, 1)
a = a.shuffle
sleep 0.5
end
live_loop :sorted do
a = (ring 6, 5, 4, 3, 2, 1)
a = a.sort
sleep 0.5
puts "sorted: ", a
end
647
However, sometimes we do want to share things across threads. For example, the current key, BPM, synth etc. In these cases, the solution is to use Sonic Pi's special thread-safe state system via the fns `get` and `set`. This is discussed later on in section 10.
648
5.7 Thread Synchronisation
5.7 Sincronizzazione dei thread

Loading…

Loading…

Glossary

Source Translation
No related strings found in the glossary.

Source information

Flags
no-wrap
Source string location
05.6-Variables.md:139
Source string age
a year ago
Translation file
etc/doc/lang/sonic-pi-tutorial-it.po, string 643
String priority
Medium
Failing checks