class Concurrent::Cancellation
Provides tools for cooperative cancellation. Inspired by <msdn.microsoft.com/en-us/library/dd537607(v=vs.110).aspx>
@example
# Create new cancellation. `cancellation` is used for cancelling, `token` is passed down to # tasks for cooperative cancellation cancellation, token = Concurrent::Cancellation.create Thread.new(token) do |token| # Count 1+1 (simulating some other meaningful work) repeatedly # until the token is cancelled through cancellation. token.loop_until_canceled { 1+1 } end sleep 0.1 cancellation.cancel # Stop the thread by cancelling
@!macro warn.edge
Public Class Methods
Creates the cancellation object. Returns both the cancellation and the token for convenience. @param [Object] resolve_args resolve_args Arguments which are used when resolve method is called on
resolvable_future_or_event
@param [Promises::Resolvable] resolvable_future_or_event resolvable used to track cancellation.
Can be retrieved by `token.to_future` ot `token.to_event`.
@example
cancellation, token = Concurrent::Cancellation.create
@return [Array(Cancellation, Cancellation::Token)]
# File lib-edge/concurrent/edge/cancellation.rb, line 29 def self.create(resolvable_future_or_event = Promises.resolvable_event, *resolve_args) cancellation = new(resolvable_future_or_event, *resolve_args) [cancellation, cancellation.token] end
# File lib-edge/concurrent/edge/cancellation.rb, line 65 def initialize(future, *resolve_args) raise ArgumentError, 'future is not Resolvable' unless future.is_a?(Promises::Resolvable) @Cancel = future @Token = Token.new @Cancel.with_hidden_resolvable @ResolveArgs = resolve_args end
Public Instance Methods
Cancel this cancellation. All executions depending on the token will cooperatively stop. @return [true, false] @raise when cancelling for the second tim
# File lib-edge/concurrent/edge/cancellation.rb, line 45 def cancel(raise_on_repeated_call = true) !!@Cancel.resolve(*@ResolveArgs, raise_on_repeated_call) end
Is the cancellation cancelled? @return [true, false]
# File lib-edge/concurrent/edge/cancellation.rb, line 51 def canceled? @Cancel.resolved? end
Short string representation. @return [String]
# File lib-edge/concurrent/edge/cancellation.rb, line 57 def to_s format '%s canceled:%s>', super[0..-2], canceled? end
Returns the token associated with the cancellation. @return [Token]
# File lib-edge/concurrent/edge/cancellation.rb, line 38 def token @Token end