项目作者: sniper00

项目描述 :
Pure lua implements timer ,support coroutine(with coroutine pool optimized)
高级语言: Lua
项目地址: git://github.com/sniper00/lua_timer.git
创建时间: 2016-12-01T09:23:50Z
项目社区:https://github.com/sniper00/lua_timer

开源协议:

下载


lua_timer

Pure lua implements timer ,support coroutine(with coroutine pool optimized)

Use

Call timer.update() in your framework’s main loop/update

  1. local os = require ('os')
  2. local timer = require ('timer')
  3. local function co3()
  4. print("coroutine3 start",os.clock())
  5. timer.sleep(5.5)
  6. print("coroutine3 5.5 second later",os.clock())
  7. print("coroutine3 end")
  8. end
  9. --coroutine style timer
  10. timer.async(function()
  11. print("coroutine1 start",os.time())
  12. timer.sleep(2)
  13. print("coroutine1 2 second later",os.time())
  14. timer.async(co3)
  15. print("coroutine1 end")
  16. end)
  17. timer.async(function()
  18. print("coroutine2 start",os.time())
  19. timer.sleep(1)
  20. print("coroutine2 1 second later",os.time())
  21. timer.sleep(1)
  22. print("coroutine2 1 second later ",os.time())
  23. timer.sleep(1)
  24. print("coroutine2 1 second later ",os.time())
  25. timer.sleep(1)
  26. print("coroutine2 1 second later ",os.time() )
  27. print("coroutine2 end")
  28. end)
  29. --callback style timer
  30. local stime = os.time()
  31. timer.timeout(1.5,function()
  32. print("timer expired", os.time() - stime)
  33. end)
  34. --remove a timer
  35. local ctx = timer.timeout(5,function()
  36. error("this timer shoud not expired")
  37. end)
  38. timer.remove(ctx)
  39. print("main thread noblock")
  40. while true do
  41. timer.update()
  42. end