class Concurrent::AbstractExchanger
@!visibility private
Constants
- CANCEL
-
@!visibility private
Public Class Methods
Source
# File lib/concurrent-ruby/concurrent/exchanger.rb, line 44 def initialize super end
Calls superclass method
Public Instance Methods
Source
# File lib/concurrent-ruby/concurrent/exchanger.rb, line 69 def exchange(value, timeout = nil) (value = do_exchange(value, timeout)) == CANCEL ? nil : value end
@!macro exchanger_method_do_exchange
Waits for another thread to arrive at this exchange point (unless the current thread is interrupted), and then transfers the given object to it, receiving its object in return. The timeout value indicates the approximate number of seconds the method should block while waiting for the exchange. When the timeout value is `nil` the method will block indefinitely. @param [Object] value the value to exchange with another thread @param [Numeric, nil] timeout in seconds, `nil` blocks indefinitely
@!macro exchanger_method_exchange
In some edge cases when a `timeout` is given a return value of `nil` may be ambiguous. Specifically, if `nil` is a valid value in the exchange it will be impossible to tell whether `nil` is the actual return value or if it signifies timeout. When `nil` is a valid value in the exchange consider using {#exchange!} or {#try_exchange} instead. @return [Object] the value exchanged by the other thread or `nil` on timeout
Source
# File lib/concurrent-ruby/concurrent/exchanger.rb, line 80 def exchange!(value, timeout = nil) if (value = do_exchange(value, timeout)) == CANCEL raise Concurrent::TimeoutError else value end end
@!macro exchanger_method_do_exchange @!macro exchanger_method_exchange_bang
On timeout a {Concurrent::TimeoutError} exception will be raised. @return [Object] the value exchanged by the other thread @raise [Concurrent::TimeoutError] on timeout
Source
# File lib/concurrent-ruby/concurrent/exchanger.rb, line 109 def try_exchange(value, timeout = nil) if (value = do_exchange(value, timeout)) == CANCEL Concurrent::Maybe.nothing(Concurrent::TimeoutError) else Concurrent::Maybe.just(value) end end
@!macro exchanger_method_do_exchange @!macro exchanger_method_try_exchange
The return value will be a {Concurrent::Maybe} set to `Just` on success or `Nothing` on timeout. @return [Concurrent::Maybe] on success a `Just` maybe will be returned with the item exchanged by the other thread as `#value`; on timeout a `Nothing` maybe will be returned with {Concurrent::TimeoutError} as `#reason` @example exchanger = Concurrent::Exchanger.new result = exchanger.exchange(:foo, 0.5) if result.just? puts result.value #=> :bar else puts 'timeout' end
Private Instance Methods
Source
# File lib/concurrent-ruby/concurrent/exchanger.rb, line 122 def do_exchange(value, timeout) raise NotImplementedError end
@!macro exchanger_method_do_exchange
@return [Object, CANCEL] the value exchanged by the other thread; {CANCEL} on timeout