A threadsafe future implementation that blocks caller until a value is set.
future = FutureValue.new
If the value is known at instantiation time, can pass the value. This is more efficient and will prevent blocking.
future = FutureValue.new(10)
future.value = 10
A RuntimeError will be raised if value is already set.
future.value = 10 unless future.has_value?
until future.has_value?
sleep 1
end
value = future.value
value = future.value
require 'future_value'
f = FutureValue.new
t = Thread.new do
sleep 2
f.value = 4
end
if f.has_value?
puts "yes"
else
puts "no"
end
3.times do |i|
Thread.new do
puts "Thread #{i} got #{f.value}"
end
end
puts f.value
puts f.has_value?
- Fork it ( https://github.com/DougEverly/future_value/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request