EventLoop_test.cpp 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * EventLoop_test.cpp
  3. *
  4. * @build
  5. * make libhv && sudo make install
  6. * g++ -std=c++11 EventLoop_test.cpp -o EventLoop_test -I/usr/local/include/hv -lhv -lpthread
  7. *
  8. */
  9. #include "hv.h"
  10. #include "EventLoop.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. EventLoopPtr loop(new EventLoop);
  19. // runEvery 1s
  20. loop->setInterval(1000, std::bind(onTimer, std::placeholders::_1, 100));
  21. // runAfter 10s
  22. loop->setTimeout(10000, [&loop](TimerID timerID){
  23. loop->stop();
  24. });
  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. // run until loop stopped
  32. loop->run();
  33. return 0;
  34. }