EventLoopThread_test.cpp 957 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * EventLoopThread_test.cpp
  3. *
  4. * @build: make evpp
  5. *
  6. */
  7. #include "hv.h"
  8. #include "EventLoopThread.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. EventLoopThread loop_thread;
  17. const EventLoopPtr& loop = loop_thread.loop();
  18. // runEvery 1s
  19. loop->setInterval(1000, std::bind(onTimer, std::placeholders::_1, 100));
  20. // runAfter 10s
  21. loop->setTimeout(10000, [&loop](TimerID timerID){
  22. loop->stop();
  23. });
  24. loop_thread.start();
  25. loop->queueInLoop([](){
  26. printf("queueInLoop tid=%ld\n", hv_gettid());
  27. });
  28. loop->runInLoop([](){
  29. printf("runInLoop tid=%ld\n", hv_gettid());
  30. });
  31. // wait loop_thread exit
  32. loop_thread.join();
  33. return 0;
  34. }