Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created October 29, 2024 22:36
Show Gist options
  • Save tenderlove/3b9a1104d9f0bf7f5d4af59d88c54218 to your computer and use it in GitHub Desktop.
Save tenderlove/3b9a1104d9f0bf7f5d4af59d88c54218 to your computer and use it in GitHub Desktop.
##
# 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