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:
Ahora hemos atrapado y recordado `s` como una variable, lo que nos permite controlar el sintetizador mientras corre:
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.
Veremos en más detalle cómo controlar los sintetizadores más tarde.
641
Warning: Variables and Threads
Advertencia: Variables e Hilos
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*:
Si bien las variables son buenas para nombrar cosas y guardar los resultados de estas, es importante saber que, por lo general, solo deben usarse localmente dentro de un hilo. Por ejemplo, *no hagas esto*:
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
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:
Hay dos soluciones a este problema. Primero, *no utilices el miso variable en multiples live loops o subprocesos*. Por ejemplo, el siguiente código imprimirá siempre una lista ordenada, porque cada live loop tiene su variable separado:
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
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.
Sin embargo, a veces queremos compartir datos entre hilos. Por ejemplo, la clave actual, la velocidad, el sintetizador etc. En estos casos, se puede utilizar el sistema especial "thread-safe" de Sonic Pi a través de los fns `get` y `set`. Esto sera discutido luego en la sección 10.
648
5.7 Thread Synchronisation
5.7 Sincronización de hilos

Loading…

Loading…

Things to check

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-es.po, string 643
String priority
Medium
Failing checks