Created
April 12, 2015 20:42
-
-
Save jjsanderson/050c3a38da050acbb724 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in 0..( p_count -1 ) do | |
in_thread(name: i) do | |
loop do | |
chime p_freq[i], dur | |
sleep p_sleep[i] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a classic "mutable state vs threads" issue. If I had my way, I'd get rid of all mutation/variables in Sonic Pi. Unfortunately, they're on the curriculum and they're everywhere in Ruby, so I'm working on ways to make alternatives easier and better!
First up, you rarely ever see the
for i in ...
pattern in Ruby. It's much more idiomatic to use iteration:Interestingly, this doesn't suffer from the same problem as your code. Clearly, the
for
comprehension uses mutable state so that all thei
's in the threads are actually the samei
not distincti
s. The.each
version appears to give distincti
s.However, you should always be wary of passing in variables into threads. The approach you're taking treats the variables as so-called free variables which in an Object Oriented world means free references as different references can refer to the same object.
Essentially, mutable state and Objects are often bad news when combined with concurrency. We need to move to a more immutable and functional world (which is where I'm slowly pushing Sonic Pi).
Hope this helps in some way.