Skip to content

Instantly share code, notes, and snippets.

@mikolajb
Created August 25, 2010 15:31
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 mikolajb/549721 to your computer and use it in GitHub Desktop.
Save mikolajb/549721 to your computer and use it in GitHub Desktop.
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 }
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