thread_exit.c 826 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. /* Start function for the background thread */
  8. void *
  9. bg_func(void *arg)
  10. {
  11. printf("Thread start.\n");
  12. while (1) {
  13. pthread_exit(NULL);
  14. }
  15. }
  16. /* Foreground thread and main entry point */
  17. int
  18. main(int argc, char *argv[])
  19. {
  20. pthread_t bg_thread;
  21. if (pthread_create(&bg_thread, NULL, bg_func, NULL)) {
  22. printf("Thread create failed");
  23. return 1;
  24. }
  25. printf("Thread created.\n");
  26. /* Wait for background thread to finish */
  27. if (pthread_join(bg_thread, NULL)) {
  28. printf("Thread join failed");
  29. return 2;
  30. }
  31. printf("Sub-thread exit.\n");
  32. printf("Test success.\n");
  33. return 0;
  34. }