ex3.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. /* Multi-thread searching.
  10. Illustrates: thread cancellation, cleanup handlers. */
  11. #include <sys/errno.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <pthread.h>
  17. /* Defines the number of searching threads */
  18. #define NUM_THREADS 5
  19. /* Function prototypes */
  20. void *search(void *);
  21. void print_it(void *);
  22. /* Global variables */
  23. pthread_t threads[NUM_THREADS];
  24. pthread_mutex_t lock;
  25. int tries;
  26. volatile int started;
  27. int libc_ex3()
  28. {
  29. int i;
  30. int pid;
  31. /* create a number to search for */
  32. pid = getpid();
  33. printf("Searching for the number = %d...\n", pid);
  34. /* Initialize the mutex lock */
  35. pthread_mutex_init(&lock, NULL);
  36. /* Create the searching threads */
  37. for (started=0; started<NUM_THREADS; started++)
  38. pthread_create(&threads[started], NULL, search, (void *)pid);
  39. /* Wait for (join) all the searching threads */
  40. for (i=0; i<NUM_THREADS; i++)
  41. pthread_join(threads[i], NULL);
  42. printf("It took %d tries to find the number.\n", tries);
  43. /* Exit the program */
  44. return 0;
  45. }
  46. #include <finsh.h>
  47. FINSH_FUNCTION_EXPORT(libc_ex3, example 5 for libc);
  48. /* This is the cleanup function that is called
  49. when the threads are cancelled */
  50. void print_it(void *arg)
  51. {
  52. int *try = (int *) arg;
  53. pthread_t tid;
  54. /* Get the calling thread's ID */
  55. tid = pthread_self();
  56. /* Print where the thread was in its search when it was cancelled */
  57. printf("Thread %lx was canceled on its %d try.\n", tid, *try);
  58. }
  59. /* This is the search routine that is executed in each thread */
  60. void *search(void *arg)
  61. {
  62. int num = (int) arg;
  63. int i, j, ntries;
  64. pthread_t tid;
  65. /* get the calling thread ID */
  66. tid = pthread_self();
  67. /* use the thread ID to set the seed for the random number generator */
  68. /* Since srand and rand are not thread-safe, serialize with lock */
  69. /* Try to lock the mutex lock --
  70. if locked, check to see if the thread has been cancelled
  71. if not locked then continue */
  72. while (pthread_mutex_trylock(&lock) == EBUSY)
  73. pthread_testcancel();
  74. srand((int)tid);
  75. i = rand() & 0xFFFFFF;
  76. pthread_mutex_unlock(&lock);
  77. ntries = 0;
  78. /* Set the cancellation parameters --
  79. - Enable thread cancellation
  80. - Defer the action of the cancellation */
  81. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  82. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  83. while (started < NUM_THREADS)
  84. sched_yield ();
  85. /* Push the cleanup routine (print_it) onto the thread
  86. cleanup stack. This routine will be called when the
  87. thread is cancelled. Also note that the pthread_cleanup_push
  88. call must have a matching pthread_cleanup_pop call. The
  89. push and pop calls MUST be at the same lexical level
  90. within the code */
  91. /* Pass address of `ntries' since the current value of `ntries' is not
  92. the one we want to use in the cleanup function */
  93. pthread_cleanup_push(print_it, (void *)&ntries);
  94. /* Loop forever */
  95. while (1) {
  96. i = (i + 1) & 0xFFFFFF;
  97. ntries++;
  98. /* Does the random number match the target number? */
  99. if (num == i) {
  100. /* Try to lock the mutex lock --
  101. if locked, check to see if the thread has been cancelled
  102. if not locked then continue */
  103. while (pthread_mutex_trylock(&lock) == EBUSY)
  104. pthread_testcancel();
  105. /* Set the global variable for the number of tries */
  106. tries = ntries;
  107. printf("Thread %lx found the number!\n", tid);
  108. /* Cancel all the other threads */
  109. for (j=0; j<NUM_THREADS; j++)
  110. if (threads[j] != tid) pthread_cancel(threads[j]);
  111. /* Break out of the while loop */
  112. break;
  113. }
  114. /* Every 100 tries check to see if the thread has been cancelled. */
  115. if (ntries % 100 == 0) {
  116. pthread_testcancel();
  117. }
  118. }
  119. /* The only way we can get here is when the thread breaks out
  120. of the while loop. In this case the thread that makes it here
  121. has found the number we are looking for and does not need to run
  122. the thread cleanup function. This is why the pthread_cleanup_pop
  123. function is called with a 0 argument; this will pop the cleanup
  124. function off the stack without executing it */
  125. pthread_cleanup_pop(0);
  126. return((void *)0);
  127. }