Method: Concurrent#try_exchange

Defined in:
lib/concurrent-ruby/concurrent/exchanger.rb

#try_exchange(value, timeout = nil) ⇒ Concurrent::Maybe

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.

The return value will be a Maybe set to Just on success or Nothing on timeout.

Examples:


exchanger = Concurrent::Exchanger.new

result = exchanger.exchange(:foo, 0.5)

if result.just?
  puts result.value #=> :bar
else
  puts 'timeout'
end

Parameters:

  • value (Object)

    the value to exchange with another thread

  • timeout (Numeric, nil) (defaults to: nil)

    in seconds, nil blocks indefinitely

Returns:

  • (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 TimeoutError as #reason



# File 'lib/concurrent-ruby/concurrent/exchanger.rb', line 349