Skip to content

Instantly share code, notes, and snippets.

@christiankakesa
Last active March 18, 2019 00:45
Show Gist options
  • Save christiankakesa/82750976453857576ef0daa1025d83de to your computer and use it in GitHub Desktop.
Save christiankakesa/82750976453857576ef0daa1025d83de to your computer and use it in GitHub Desktop.
Example of complete ticker
struct Ticker
getter tick, done
@tick = Channel(Bool).new
@done = Channel(Bool).new
def initialize(tick_s : Int32)
spawn do
loop do
sleep tick_s
@tick.send true
end
end
end
def stop!
@done.send true
end
end
ticker = Ticker.new(2) # 2 seconds
spawn do
loop do
select
when ticker.tick.receive
puts "#{Time.now}"
when ticker.done.receive
puts "break!!!"
break
end
end
end
sleep 7
ticker.stop!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment