1
0

EventLoopThread_test.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * EventLoopThread_test.cpp
  3. *
  4. * @build
  5. * make libhv && sudo make install
  6. * g++ -std=c++11 EventLoopThread_test.cpp -o EventLoopThread_test -I/usr/local/include/hv -lhv -lpthread
  7. *
  8. */
  9. #include "hv.h"
  10. #include "EventLoopThread.h"
  11. using namespace hv;
  12. static void onTimer(TimerID timerID, int n) {
  13. printf("tid=%ld timerID=%lu time=%lus n=%d\n", hv_gettid(), (unsigned long)timerID, (unsigned long)time(NULL), n);
  14. }
  15. int main(int argc, char* argv[]) {
  16. HV_MEMCHECK;
  17. printf("main tid=%ld\n", hv_gettid());
  18. EventLoopThread loop_thread;
  19. EventLoopPtr loop = loop_thread.loop();
  20. // runEvery 1s
  21. loop->setInterval(1000, std::bind(onTimer, std::placeholders::_1, 100));
  22. // runAfter 10s
  23. loop->setTimeout(10000, [&loop](TimerID timerID){
  24. loop->stop();
  25. });
  26. loop_thread.start();
  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. // wait loop_thread exit
  34. loop_thread.join();
  35. return 0;
  36. }