Method: Concurrent::Agent#initialize

Defined in:
lib/concurrent/agent.rb

#initialize(initial, opts = {}) ⇒ Agent

Initialize a new Agent with the given initial value and provided options.

Parameters:

  • initial (Object)

    the initial value

  • opts (Hash) (defaults to: {})

    the options used to define the behavior at update and deref

Options Hash (opts):

  • :operation (Boolean) — default: false

    when true will execute the future on the global operation pool (for long-running operations), when false will execute the future on the global task pool (for short-running tasks)

  • :executor (object)

    when provided will run all operations on this executor rather than the global thread pool (overrides :operation)

  • :dup_on_deref (String) — default: false

    call #dup before returning the data

  • :freeze_on_deref (String) — default: false

    call #freeze before returning the data

  • :copy_on_deref (String) — default: nil

    call the given Proc passing the internal value and returning the value returned from the proc



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/concurrent/agent.rb', line 37

def initialize(initial, opts = {})
  @value                = initial
  @rescuers             = []
  @validator            = Proc.new { |result| true }
  self.observers        = CopyOnWriteObserverSet.new
  @serialized_execution = SerializedExecution.new
  @task_executor        = OptionsParser.get_task_executor_from(opts)
  @operation_executor   = OptionsParser.get_operation_executor_from(opts)
  init_mutex
  set_deref_options(opts)
end