Class: ThreadUseGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/util/thread_use_generator.rb

Overview

スレッドを使ったGenerator

Instance Method Summary collapse

Constructor Details

#initialize(enum, buff_size = nil) ⇒ ThreadUseGenerator

Returns a new instance of ThreadUseGenerator.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jiji/util/thread_use_generator.rb', line 5

def initialize( enum, buff_size=nil )
  @alive = true
  @alive_mutex = Mutex.new
  @q = buff_size ? SizedQueue.new( buff_size ) : Queue.new

  @end = Object.new
  @t = Thread.fork {
    begin
      enum.each {|*items|
        break unless @alive_mutex.synchronize { @alive }
        @q << items
      }
    ensure
      @q << @end
    end
  }
  Thread.pass

  @has_next = true
  inner_next
end

Instance Method Details

#closeObject



38
39
40
41
42
43
44
45
# File 'lib/jiji/util/thread_use_generator.rb', line 38

def close
  @alive_mutex.synchronize {
    @alive = false
  }
  @has_next = false
  @q.clear
  @t.join
end

#nextObject



30
31
32
33
34
35
36
37
# File 'lib/jiji/util/thread_use_generator.rb', line 30

def next
  raise "illegal state." unless next?
  begin
    @next_element
  ensure
    inner_next
  end
end

#next?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/jiji/util/thread_use_generator.rb', line 27

def next?
  @has_next
end