1 /* Multi-thread searching.
2 Illustrates: thread cancellation, cleanup handlers. */
11 /* Defines the number of searching threads */
14 /* Function prototypes */
16 void print_it(void *);
18 /* Global variables */
19 pthread_t threads
[NUM_THREADS
];
24 int main(int argc
, char ** argv
)
29 /* create a number to search for */
31 printf("Searching for the number = %d...\n", pid
);
33 /* Initialize the mutex lock */
34 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
);
40 /* Wait for (join) all the searching threads */
41 for (i
=0; i
<NUM_THREADS
; i
++)
42 pthread_join(threads
[i
], NULL
);
44 printf("It took %d tries to find the number.\n", tries
);
46 /* Exit the program */
50 /* This is the cleanup function that is called
51 when the threads are cancelled */
53 void print_it(void *arg
)
55 int *try = (int *) arg
;
58 /* Get the calling thread's ID */
61 /* Print where the thread was in its search when it was cancelled */
62 printf("Thread %lx was canceled on its %d try.\n", tid
, *try);
65 /* This is the search routine that is executed in each thread */
67 void *search(void *arg
)
73 /* get the calling thread ID */
76 /* use the thread ID to set the seed for the random number generator */
77 /* Since srand and rand are not thread-safe, serialize with lock */
79 /* Try to lock the mutex lock --
80 if locked, check to see if the thread has been cancelled
81 if not locked then continue */
82 while (pthread_mutex_trylock(&lock
) == EBUSY
)
86 i
= rand() & 0xFFFFFF;
87 pthread_mutex_unlock(&lock
);
90 /* Set the cancellation parameters --
91 - Enable thread cancellation
92 - Defer the action of the cancellation */
94 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
95 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED
, NULL
);
97 while (started
< NUM_THREADS
)
100 /* Push the cleanup routine (print_it) onto the thread
101 cleanup stack. This routine will be called when the
102 thread is cancelled. Also note that the pthread_cleanup_push
103 call must have a matching pthread_cleanup_pop call. The
104 push and pop calls MUST be at the same lexical level
107 /* Pass address of `ntries' since the current value of `ntries' is not
108 the one we want to use in the cleanup function */
110 pthread_cleanup_push(print_it
, (void *)&ntries
);
114 i
= (i
+ 1) & 0xFFFFFF;
117 /* Does the random number match the target number? */
119 /* Try to lock the mutex lock --
120 if locked, check to see if the thread has been cancelled
121 if not locked then continue */
122 while (pthread_mutex_trylock(&lock
) == EBUSY
)
123 pthread_testcancel();
125 /* Set the global variable for the number of tries */
127 printf("Thread %lx found the number!\n", tid
);
129 /* Cancel all the other threads */
130 for (j
=0; j
<NUM_THREADS
; j
++)
131 if (threads
[j
] != tid
) pthread_cancel(threads
[j
]);
133 /* Break out of the while loop */
137 /* Every 100 tries check to see if the thread has been cancelled. */
138 if (ntries
% 100 == 0) {
139 pthread_testcancel();
143 /* The only way we can get here is when the thread breaks out
144 of the while loop. In this case the thread that makes it here
145 has found the number we are looking for and does not need to run
146 the thread cleanup function. This is why the pthread_cleanup_pop
147 function is called with a 0 argument; this will pop the cleanup
148 function off the stack without executing it */
150 pthread_cleanup_pop(0);