Class: Cute::Synchronization::SlidingWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/cute/synchronization.rb

Instance Method Summary collapse

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

#runObject



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