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.
41 42 43 44 45 46 |
# File 'lib/cute/synchronization.rb', line 41 def initialize(size) @queue = [] @lock = Mutex.new @finished = false @size = size end |
Instance Method Details
#add(t) ⇒ Object
48 49 50 |
# File 'lib/cute/synchronization.rb', line 48 def add(t) @queue << t end |
#run ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cute/synchronization.rb', line 52 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 |