-
-
Save tenderlove/3b9a1104d9f0bf7f5d4af59d88c54218 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
## | |
# Demonstration of a race condition when using Async. | |
require "async" | |
Async { | |
x = Async { | |
5.times do | |
@val = 2 | |
sleep rand(0.1) # simulate random amounts of IO | |
p({ 2 => @val }) # what is @val? | |
end | |
} | |
y = Async { | |
5.times do | |
@val = 3 | |
sleep rand(0.1) # simulate random amounts of IO | |
p({ 3 => @val }) # what is @val? | |
end | |
} | |
z = Async { | |
5.times do | |
@val = 4 | |
sleep rand(0.1) # simulate random amounts of IO | |
p({ 4 => @val }) # what is @val? | |
end | |
} | |
x.wait | |
y.wait | |
z.wait | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment