1
0

EventLoop_test.cpp 876 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * EventLoop_test.cpp
  3. *
  4. * @build: make evpp
  5. *
  6. */
  7. #include "hv.h"
  8. #include "EventLoop.h"
  9. using namespace hv;
  10. static void onTimer(TimerID timerID, int n) {
  11. printf("tid=%ld timerID=%lu time=%lus n=%d\n", hv_gettid(), (unsigned long)timerID, (unsigned long)time(NULL), n);
  12. }
  13. int main(int argc, char* argv[]) {
  14. HV_MEMCHECK;
  15. printf("main tid=%ld\n", hv_gettid());
  16. auto loop = std::make_shared<EventLoop>();
  17. // runEvery 1s
  18. loop->setInterval(1000, std::bind(onTimer, std::placeholders::_1, 100));
  19. // runAfter 10s
  20. loop->setTimeout(10000, [&loop](TimerID timerID){
  21. loop->stop();
  22. });
  23. loop->queueInLoop([](){
  24. printf("queueInLoop tid=%ld\n", hv_gettid());
  25. });
  26. loop->runInLoop([](){
  27. printf("runInLoop tid=%ld\n", hv_gettid());
  28. });
  29. // run until loop stopped
  30. loop->run();
  31. return 0;
  32. }