Class: RImageAnalysisTools::ThreadQueue
- Inherits:
-
Object
- Object
- RImageAnalysisTools::ThreadQueue
- Defined in:
- lib/rimageanalysistools/thread_queue.rb
Instance Attribute Summary collapse
-
#max_threads ⇒ Object
Returns the value of attribute max_threads.
-
#running ⇒ Object
readonly
Returns the value of attribute running.
Class Method Summary collapse
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #enqueue(&b) ⇒ Object
- #finish ⇒ Object
- #has_running_threads? ⇒ Boolean
-
#initialize ⇒ ThreadQueue
constructor
A new instance of ThreadQueue.
- #remove_completed_threads ⇒ Object
- #run_thread_if_space ⇒ Object
- #start_queue ⇒ Object
Constructor Details
#initialize ⇒ ThreadQueue
Returns a new instance of ThreadQueue.
38 39 40 41 42 43 44 45 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 38 def initialize @threads = Array.new @waiting = Array.new @mut = Mutex.new @max_threads = 4 @running = false @results = [] end |
Instance Attribute Details
#max_threads ⇒ Object
Returns the value of attribute max_threads.
121 122 123 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 121 def max_threads @max_threads end |
#running ⇒ Object (readonly)
Returns the value of attribute running.
47 48 49 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 47 def running @running end |
Class Method Details
.new_scope_with_vars(*args) {|args| ... } ⇒ Object
32 33 34 35 36 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 32 def self.new_scope_with_vars(*args) yield *args end |
Instance Method Details
#empty? ⇒ Boolean
129 130 131 132 133 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 129 def empty? @mut.synchronize do @threads.empty? and @waiting.empty? end end |
#enqueue(&b) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 49 def enqueue(&b) @mut.synchronize do @waiting << b end end |
#finish ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 111 def finish until empty? do remove_completed_threads sleep 0.5 end @results end |
#has_running_threads? ⇒ Boolean
123 124 125 126 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 123 def has_running_threads? remove_completed_threads not @threads.empty? end |
#remove_completed_threads ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 92 def remove_completed_threads dead = Array.new @threads.each do |t| if not t.alive? then dead << t end end @mut.synchronize do dead.each do |d| @results << d.value @threads.delete(d) end end end |
#run_thread_if_space ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 79 def run_thread_if_space @mut.synchronize do while (not @threads.size >= @max_threads) and (not @waiting.empty?) do next_task = @waiting.pop @threads << Thread.new(next_task) do |task| task.call end end end end |
#start_queue ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rimageanalysistools/thread_queue.rb', line 57 def start_queue return nil if @running @running = true Thread.new do until empty? do run_thread_if_space sleep 0.2 end @running = false end end |