Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dougeverly/futurevalue
https://github.com/dougeverly/futurevalue
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/dougeverly/futurevalue
- Owner: DougEverly
- License: mit
- Created: 2015-03-27T16:09:44.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-07-09T22:49:25.000Z (over 9 years ago)
- Last Synced: 2023-03-11T20:48:11.861Z (almost 2 years ago)
- Language: Ruby
- Size: 141 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# FutureValue
A threadsafe future implementation that blocks caller until a value is set.
## Usage
### Create a future value object
future = FutureValue.new
### Create a future value object with valueIf the value is known at instantiation time, can pass the value. This is more efficient and will prevent blocking.
future = FutureValue.new(10)
### Set a value
future.value = 10
A RuntimeError will be raised if value is already set.
future.value = 10 unless future.has_value?
### Get a value (non-blocking)until future.has_value?
sleep 1
end
value = future.value
### Get a value (blocking)value = future.value
## Example
require 'future_value'
f = FutureValue.new
t = Thread.new do
sleep 2
f.value = 4
endif f.has_value?
puts "yes"
else
puts "no"
end3.times do |i|
Thread.new do
puts "Thread #{i} got #{f.value}"
end
endputs f.value
puts f.has_value?
## Contributing
1. Fork it ( https://github.com/DougEverly/future_value/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request