thread_check.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 2004-2022 Free Software Foundation, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. This file was written by Steve Munroe (sjmunroe@us.ibm.com). */
  13. /* Test break points and single step on thread functions. */
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <pthread.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #define N 2
  21. static void *
  22. tf (void *arg)
  23. {
  24. int n = (int) (long int) arg;
  25. char number[160];
  26. int unslept = 10;
  27. sprintf(number, "tf(%ld): begin", (long)arg);
  28. puts (number);
  29. while (unslept > 0)
  30. unslept = sleep(unslept);
  31. sprintf(number, "tf(%ld): end", (long)arg);
  32. puts (number);
  33. return NULL;
  34. }
  35. int main (int argc, char *argv[])
  36. {
  37. int n;
  38. int unslept = 2;
  39. pthread_t th[N];
  40. for (n = 0; n < N; ++n)
  41. if (pthread_create (&th[n], NULL, tf, (void *) (long int) n) != 0)
  42. {
  43. while (unslept > 0)
  44. unslept = sleep(2);
  45. puts ("create failed");
  46. exit (1);
  47. }
  48. puts("after create");
  49. for (n = 0; n < N; ++n)
  50. if (pthread_join (th[n], NULL) != 0)
  51. {
  52. puts ("join failed");
  53. exit (1);
  54. }
  55. puts("after join");
  56. return 0;
  57. }