Skip to content

Instantly share code, notes, and snippets.

@joonty
Created July 15, 2013 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonty/6000969 to your computer and use it in GitHub Desktop.
Save joonty/6000969 to your computer and use it in GitHub Desktop.
Test for two threads accessing the same array, one thread iterating and the other appending.
iterations = 0
payloads = (1..1000).to_a
threads = []
threads << Thread.new do
200.times do
add_num = rand(1000..2000)
puts "\nAdding #{add_num} to payload"
payloads << add_num
end
end
threads << Thread.new do
payloads.each do |p|
iterations += 1
puts "Payload: #{p}"
end
end
threads.each(&:join)
puts "\n---"
puts "Iterations: #{iterations}"
puts "Final payload length: #{payloads.length}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment