EventLoop_test.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * EventLoop_test.cpp
  3. *
  4. * @build
  5. * make libhv && sudo make install
  6. * g++ -std=c++11 EventLoop_test.cpp -o EventLoop_test -I/usr/local/include/hv -lhv
  7. *
  8. */
  9. #include "hbase.h" // import HV_MEMCHECK
  10. #include "EventLoop.h"
  11. using namespace hv;
  12. static void onTimer(TimerID timerID, EventLoop* loop, int n) {
  13. static int cnt = 0;
  14. printf("n=%d timerID=%lu time=%lus\n", n, (unsigned long)timerID, (unsigned long)time(NULL));
  15. if (++cnt == 15) {
  16. printf("killTimer(%ld)\n", (unsigned long)timerID);
  17. loop->killTimer(timerID);
  18. }
  19. }
  20. int main(int argc, char* argv[]) {
  21. HV_MEMCHECK;
  22. EventLoop loop;
  23. int n = 100;
  24. loop.setInterval(1000, std::bind(onTimer, std::placeholders::_1, &loop, n));
  25. loop.setTimeout(10000, [&loop](TimerID timerID){
  26. static int cnt = 0;
  27. if (cnt++ == 0) {
  28. printf("resetTimer(%ld)\n", (unsigned long)timerID);
  29. loop.resetTimer(timerID);
  30. } else {
  31. loop.stop();
  32. }
  33. });
  34. printf("tid=%ld\n", hv_gettid());
  35. loop.queueInLoop([](){
  36. printf("queueInLoop tid=%ld\n", hv_gettid());
  37. });
  38. loop.runInLoop([](){
  39. printf("runInLoop tid=%ld\n", hv_gettid());
  40. });
  41. loop.start();
  42. return 0;
  43. }