An IVar
is like a future that you can assign. As a future is a value that is being computed that you can wait on, an IVar
is a value that is waiting to be assigned, that you can wait on. IVars
are single assignment and deterministic.
Then, express futures as an asynchronous computation that assigns an IVar
. The IVar
becomes the primitive on which futures and dataflow are built.
An IVar
is a single-element container that is normally created empty, and can only be set once. The I in IVar
stands for immutable. Reading an IVar
normally blocks until it is set. It is safe to set and read an IVar
from different threads.
If you want to have some parallel task set the value in an IVar
, you want a Future
. If you want to create a graph of parallel tasks all executed when the values they depend on are ready you want dataflow
. IVar
is generally a low-level primitive.
Create, set and get an IVar
ivar = Concurrent::IVar.new
ivar.set 14
ivar.value ivar.set 2
See Also
Add an observer on this object that will receive notification on update.
Set the IVar
to failed due to some error and wake or notify all threads waiting on it.
Create a new IVar
in the :pending
state with the (optional) initial value.
Set the IVar
to a value and wake or notify all threads waiting on it.
Attempt to set the IVar
with the given value or block.
Has the obligation completed processing?.
Return the number of observers associated with this object.
Remove observer
as an observer on this object so that it will no longer receive notifications.
Remove all observers associated with this object.
Has the obligation been fulfilled?.
Is the obligation still awaiting completion of processing?.
Is obligation completion still pending?.
If an exception was raised during processing this will return the exception object.
Has the obligation been rejected?.
The current state of the obligation.
Is the obligation still unscheduled?.
The current value of the obligation.
The current value of the obligation.
Wait until obligation is complete or the timeout has been reached.
Wait until obligation is complete or the timeout is reached.
As #add_observer
but can be used for chaining.
Create a new IVar
in the :pending
state with the (optional) initial value.
62 63 64 65 66 67 68
# File 'lib/concurrent-ruby/concurrent/ivar.rb', line 62 def initialize(value = NULL, opts = {}, &block) if value != NULL && block_given? raise ArgumentError.new('provide only a value or a block') end super(&nil) synchronize { ns_initialize(value, opts, &block) } endInstance Method Details #add_observer(observer = nil, func = :update, &block) ⇒ undocumented
Add an observer on this object that will receive notification on update.
Upon completion the IVar
will notify all observers in a thread-safe way. The func
method of the observer will be called with three arguments: the Time
at which the Future
completed the asynchronous operation, the final value
(or nil
on rejection), and the final reason
(or nil
on fulfillment).
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
# File 'lib/concurrent-ruby/concurrent/ivar.rb', line 81 def add_observer(observer = nil, func = :update, &block) raise ArgumentError.new('cannot provide both an observer and a block') if observer && block direct_notification = false if block observer = block func = :call end synchronize do if event.set? direct_notification = true else observers.add_observer(observer, func) end end observer.send(func, Time.now, self.value, reason) if direct_notification observer end#fail(reason = StandardError.new) ⇒ IVar
Set the IVar
to failed due to some error and wake or notify all threads waiting on it.
135 136 137
# File 'lib/concurrent-ruby/concurrent/ivar.rb', line 135 def fail(reason = StandardError.new) complete(false, nil, reason) end#set(value = NULL) { ... } ⇒ IVar
Set the IVar
to a value and wake or notify all threads waiting on it.
113 114 115 116 117 118 119 120 121 122 123 124 125 126
# File 'lib/concurrent-ruby/concurrent/ivar.rb', line 113 def set(value = NULL) check_for_block_or_value!(block_given?, value) raise MultipleAssignmentError unless compare_and_set_state(:processing, :pending) begin value = yield if block_given? complete_without_notification(true, value, nil) rescue => ex complete_without_notification(false, nil, ex) end notify_observers(self.value, reason) self end#try_set(value = NULL) { ... } ⇒ Boolean
Attempt to set the IVar
with the given value or block. Return a boolean indicating the success or failure of the set operation.
145 146 147 148 149 150
# File 'lib/concurrent-ruby/concurrent/ivar.rb', line 145 def try_set(value = NULL, &block) set(value, &block) true rescue MultipleAssignmentError false end#complete? ⇒ Boolean Originally defined in module Concern::Obligation
Has the obligation completed processing?
#count_observers ⇒ Integer Originally defined in module Concern::ObservableReturn the number of observers associated with this object.
#delete_observer(observer) ⇒ Object Originally defined in module Concern::ObservableRemove observer
as an observer on this object so that it will no longer receive notifications.
Has the obligation been fulfilled?
#incomplete? ⇒ Boolean Originally defined in module Concern::ObligationIs the obligation still awaiting completion of processing?
#pending? ⇒ Boolean Originally defined in module Concern::ObligationIs obligation completion still pending?
#reason ⇒ Exception Originally defined in module Concern::ObligationIf an exception was raised during processing this will return the exception object. Will return nil
when the state is pending or if the obligation has been successfully fulfilled.
Has the obligation been rejected?
#state ⇒ Symbol Originally defined in module Concern::ObligationThe current state of the obligation.
#unscheduled? ⇒ Boolean Originally defined in module Concern::ObligationIs the obligation still unscheduled?
#value(timeout = nil) ⇒ Object Originally defined in module Concern::ObligationThe current value of the obligation. Will be nil
while the state is pending or the operation has been rejected.
The current value of the obligation. Will be nil
while the state is pending or the operation has been rejected. Will re-raise any exceptions raised during processing (but will not raise an exception on timeout).
Wait until obligation is complete or the timeout has been reached.
#wait!(timeout = nil) ⇒ Obligation Also known as: no_error! Originally defined in module Concern::ObligationWait until obligation is complete or the timeout is reached. Will re-raise any exceptions raised during processing (but will not raise an exception on timeout).
#with_observer(observer = nil, func = :update, &block) ⇒ Observable Originally defined in module Concern::ObservableAs #add_observer
but can be used for chaining.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4