EventLoopThreadPool_test.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * EventLoopThreadPool_test.cpp
  3. *
  4. * @build: make evpp
  5. *
  6. */
  7. #include "hv.h"
  8. #include "EventLoopThreadPool.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. hlog_set_level(LOG_LEVEL_DEBUG);
  16. printf("main tid=%ld\n", hv_gettid());
  17. EventLoopThreadPool loop_threads(4);
  18. loop_threads.start(true);
  19. int thread_num = loop_threads.threadNum();
  20. for (int i = 0; i < thread_num; ++i) {
  21. EventLoopPtr loop = loop_threads.nextLoop();
  22. printf("worker[%d] tid=%ld\n", i, loop->tid());
  23. loop->runInLoop([loop](){
  24. // runEvery 1s
  25. loop->setInterval(1000, std::bind(onTimer, std::placeholders::_1, 100));
  26. });
  27. loop->queueInLoop([](){
  28. printf("queueInLoop tid=%ld\n", hv_gettid());
  29. });
  30. loop->runInLoop([](){
  31. printf("runInLoop tid=%ld\n", hv_gettid());
  32. });
  33. }
  34. // runAfter 10s
  35. loop_threads.loop()->setTimeout(10000, [&loop_threads](TimerID timerID){
  36. loop_threads.stop(false);
  37. });
  38. // wait loop_threads exit
  39. loop_threads.join();
  40. return 0;
  41. }