Created
August 25, 2010 15:31
-
-
Save mikolajb/549721 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
require "pstore" | |
threads = [] | |
100.times do |i| | |
threads << Thread.new do | |
Thread.current[:name] = "reader#{i}" | |
p = PStore.new "test-pstore.pstore", true | |
10.times do | |
roots_empty = false | |
p.transaction do | |
roots = p.roots.select { |c| p[c] == :working} | |
roots_empty = roots.empty? | |
unless roots_empty | |
code = roots.sample | |
puts "#{Thread.current[:name]} #{code} was #{p[code].inspect}" | |
p[code] = :ready | |
puts "#{Thread.current[:name]} #{code} ready" | |
end | |
end | |
if roots_empty | |
# puts "#{Thread.current[:name]} roots empty" | |
sleep 0.05 | |
redo | |
end | |
end | |
puts "#{Thread.current[:name]} finishing" | |
end | |
end | |
threads.each { |t| t.join } |
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
require "pstore" | |
threads = [] | |
100.times do |i| | |
threads << Thread.new do | |
Thread.current[:name] = "writer#{i}" | |
p = PStore.new "test-pstore.pstore" | |
10.times do | |
code = (('a'..'z').to_a + 0.upto(10).to_a).shuffle.first(20).join | |
p.transaction do | |
p[code] = :working | |
end | |
loop do | |
ready = false | |
p.transaction do | |
if p[code] == :ready | |
puts "#{Thread.current[:name]} #{code} value is #{p[code].inspect}" | |
p.delete code | |
ready = true | |
end | |
end | |
unless ready | |
# puts "thread #{Thread.current[:name]} waiting for #{code}" | |
sleep 0.05 | |
else | |
break | |
end | |
end | |
end | |
puts "#{Thread.current[:name]} finishing" | |
end | |
end | |
threads.each { |t| t.join } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment