Module: Thread::InheritableThreadVars::ThreadClassExtensions

Included in:
Thread
Defined in:
inheritable-thread-vars-0.0.3/lib/inheritable_thread_vars.rb

Overview

If we try to perform a simpler approach to setting the parent via overriding Thread#initialize, we run into the following error: ThreadError:

uninitialized thread - check 'Thread#initialize'

So instead we attack it higher up in by prepending an override to Thread.new and leaving Thread#initialize alone.

Instance Method Summary collapse

Instance Method Details

#new(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'inheritable-thread-vars-0.0.3/lib/inheritable_thread_vars.rb', line 16

def new(*, **, &block)
  parent = Thread.current

  super do |*args|
    child = Thread.current

    if child.instance_variable_defined?(:@thread_parent)
      # :nocov:
      raise "@thread_parent has already been declared elsewhere. Bailing out instead of clobbering it!"
      # :nocov:
    end

    child.instance_variable_set("@thread_parent", parent)

    block.call(*args)
  end
end