synchronized_test.cpp 540 B

123456789101112131415161718192021222324
  1. #include "hthread.h"
  2. #include "hmutex.h"
  3. #define THREAD_NUM 10
  4. std::mutex g_mutex;
  5. HTHREAD_ROUTINE(test_synchronized) {
  6. synchronized(g_mutex) {
  7. hv_delay(1000);
  8. printf("tid=%ld time=%llus\n", hv_gettid(), (unsigned long long)time(NULL));
  9. }
  10. return 0;
  11. }
  12. int main() {
  13. hthread_t threads[THREAD_NUM];
  14. for (int i = 0; i < THREAD_NUM; ++i) {
  15. threads[i] = hthread_create(test_synchronized, NULL);
  16. }
  17. for (int i = 0; i < THREAD_NUM; ++i) {
  18. hthread_join(threads[i]);
  19. }
  20. return 0;
  21. }