Class: Async::IO::Threads

Inherits:
Object
  • Object
show all
Defined in:
lib/async/io/threads.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil) ⇒ Threads

Returns a new instance of Threads.



11
12
13
# File 'lib/async/io/threads.rb', line 11

def initialize(parent: nil)
  @parent = parent
end

Instance Method Details

#async(parent: (@parent or Task.current)) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/async/io/threads.rb', line 16

def async(parent: (@parent or Task.current))
  parent.async do
    thread = ::Thread.new do
      yield
    end
    
    thread.join
  rescue Stop
    if thread&.alive?
      thread.raise(Stop)
    end
    
    begin
      thread.join
    rescue Stop
      # Ignore.
    end
  end
end