hthread_test.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "hthread.h"
  2. #include "htime.h"
  3. HTHREAD_ROUTINE(test_thread1) {
  4. int cnt = 10;
  5. while (cnt-- > 0) {
  6. printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
  7. hv_msleep(100);
  8. }
  9. return 0;
  10. }
  11. class TestThread2 : public HThread {
  12. protected:
  13. virtual void run() {
  14. int cnt = 10;
  15. while (cnt-- > 0) {
  16. printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
  17. hv_msleep(100);
  18. }
  19. }
  20. };
  21. class TestThread3 : public HThread {
  22. protected:
  23. virtual bool doPrepare() {
  24. printf("doPrepare\n");
  25. return true;
  26. }
  27. virtual void doTask() {
  28. printf("tid=%ld time=%llums\n", hv_gettid(), gettimeofday_ms());
  29. }
  30. virtual bool doFinish() {
  31. printf("doFinish\n");
  32. return true;
  33. }
  34. };
  35. int main() {
  36. printf("c-style hthread_create\n");
  37. hthread_t thread1 = hthread_create(test_thread1, NULL);
  38. hthread_join(thread1);
  39. printf("cpp-style override HThread::run\n");
  40. TestThread2 thread2;
  41. thread2.start();
  42. thread2.stop();
  43. printf("cpp-style override HThread::doTask\n");
  44. TestThread3 thread3;
  45. thread3.setSleepPolicy(HThread::SLEEP_UNTIL, 100);
  46. thread3.start();
  47. hv_sleep(1);
  48. thread3.stop();
  49. return 0;
  50. }