1
0

EventLoopThreadPool_test.cpp 1.2 KB

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