pthread_cond.c 919 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (C) 2023 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <pthread.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. pthread_mutex_t mutex;
  9. static pthread_cond_t cond;
  10. typedef struct test {
  11. int test1;
  12. int test2;
  13. } Test;
  14. Test t1;
  15. void *
  16. thread(void *arg)
  17. {
  18. pthread_mutex_lock(&mutex);
  19. printf("thread signal\n");
  20. pthread_cond_signal(&cond);
  21. pthread_mutex_unlock(&mutex);
  22. return NULL;
  23. }
  24. int
  25. main()
  26. {
  27. pthread_t p;
  28. pthread_mutex_init(&mutex, NULL);
  29. pthread_cond_init(&cond, NULL);
  30. printf("parent begin\n");
  31. pthread_mutex_lock(&mutex);
  32. pthread_create(&p, NULL, thread, NULL);
  33. pthread_cond_wait(&cond, &mutex);
  34. pthread_mutex_unlock(&mutex);
  35. printf("parend end\n");
  36. pthread_join(p, NULL);
  37. pthread_cond_destroy(&cond);
  38. pthread_mutex_destroy(&mutex);
  39. return 0;
  40. }