Class: Cute::Synchronization::SlidingWindow
- Inherits:
-
Object
- Object
- Cute::Synchronization::SlidingWindow
- Defined in:
- lib/cute/synchronization.rb
Instance Method Summary collapse
- #add(t) ⇒ Object
-
#initialize(size) ⇒ SlidingWindow
constructor
A new instance of SlidingWindow.
- #run ⇒ Object
Constructor Details
#initialize(size) ⇒ SlidingWindow
Returns a new instance of SlidingWindow.
43 44 45 46 47 48 |
# File 'lib/cute/synchronization.rb', line 43 def initialize(size) @queue = [] @lock = Mutex.new @finished = false @size = size end |
Instance Method Details
#add(t) ⇒ Object
50 51 52 |
# File 'lib/cute/synchronization.rb', line 50 def add(t) @queue << t end |
#run ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cute/synchronization.rb', line 54 def run tids = [] (1..@size).each { tids << Thread.new { while !@finished do task = nil @lock.synchronize { if @queue.size > 0 task = @queue.pop else @finished = true end } if task if task.is_a?(Proc) task.call else system(task) end end end } } tids.each { |tid| tid.join } end |