https://github.com/matsumotory/mruby-timer-thread
Simple Timer Thread
https://github.com/matsumotory/mruby-timer-thread
mruby thread timer
Last synced: 10 months ago
JSON representation
Simple Timer Thread
- Host: GitHub
- URL: https://github.com/matsumotory/mruby-timer-thread
- Owner: matsumotory
- License: other
- Created: 2017-02-24T06:20:47.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-31T04:44:32.000Z (almost 8 years ago)
- Last Synced: 2024-10-18T18:16:55.997Z (over 1 year ago)
- Topics: mruby, thread, timer
- Language: C
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 5
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-mruby - mruby-timer-thread - Simple Timer Thread. (Threads)
README
# mruby-timer-thread [](https://travis-ci.org/matsumotory/mruby-timer-thread)
Simple Timer Thread class
## install by mrbgems
- add conf.gem line to `build_config.rb`
```ruby
MRuby::Build.new do |conf|
# ... (snip) ...
conf.gem :github => 'matsumotory/mruby-timer-thread'
end
```
## example
- non blocking timer
```ruby
timer_msec = 5000
# 5sec timer
timer = Timer::MRubyThread.new
timer.run timer_msec
while timer.running? do
sleep 1
puts "master thread sleeping loop"
end
puts "timer thread finish."
```
- blocking timer
```ruby
timer = Timer::MRubyThread.new
timer.run 3000
puts "main thread sleeping..."
sleep 1
puts "waiting timer"
timer.blocking_handler do
puts "finish timer"
end
puts "finish main thread"
```
- POSIX timer
- NOTE: POSIX timer not available on MacOS
```ruby
# Specify signal name if you want (default ot SIGALRM as timer_create's default)
# signal: nil will send no signal.
# you can add handlers with mruby-signal or mruby-signal-thread
timer = Timer::POSIX.new(signal: nil)
timer.run 5000
# You can create multiple timers (and are thread-safe by kernel)
sleep 1
timer2 = Timer::POSIX.new(signal: nil)
timer2.run 5000
while timer.running? or timer2.running? do
sleep 1
puts "Timer1 running: #{timer.running?} and Timer2 running: #{timer2.running?}"
end
# ...
```
- POSIX timer with interval
```ruby
# recommended mruby-signal-thread
SignalThread.trap :USR2 do
puts "Current datetime: #{`date`.chomp}"
end
timer = Timer::POSIX.new(signal: :USR2)
# Invoke first timer after 5,000 msec,
# then start interval timer every after 3,000 msec
# like uv_timet_t or timer_create API
timer.run 5000, 3000
Current datetime: Mon Mar 27 07:25:31 UTC 2017
Current datetime: Mon Mar 27 07:25:34 UTC 2017
Current datetime: Mon Mar 27 07:25:37 UTC 2017
Current datetime: Mon Mar 27 07:25:40 UTC 2017
Current datetime: Mon Mar 27 07:25:43 UTC 2017
...
```
## License
under the MIT License:
- see LICENSE file