ex3.c 4.1 KB

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