Class: Clearsight::Timer

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

Instance Method Summary collapse

Constructor Details

#initialize(interval, &handler) ⇒ Timer

Returns a new instance of Timer.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/clearsight.rb', line 8

def initialize(interval, &handler)
  @run = true
  @semaphore = Mutex.new

  @th = Thread.new do
    t = Time.now
    while run?
      t += interval
      (sleep(t - Time.now) rescue nil) and handler.call rescue nil
    end
  end
end

Instance Method Details

#stopObject



21
22
23
24
25
26
# File 'lib/clearsight.rb', line 21

def stop
  @semaphore.synchronize do
    @run = false
  end
  @th.join
end