Slide 62
Slide 62 text
Concurrent::Agent
def next_fibonacci(set = nil)
return [0, 1] if set.nil?
set + [set[-2..-1].reduce{|sum,x| sum + x }]
end
# create an agent with an initial value
agent = Concurrent::Agent.new(next_fibonacci)
# send a few update requests
5.times do
agent.send{|set| next_fibonacci(set) }
end
# wait for them to complete
agent.await
# get the current value
agent.value #=> [0, 1, 1, 2, 3, 5, 8]
Thread-safe variable