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
];
23 int main(int argc
, char ** argv
)
28 /* create a number to search for */
30 printf("Searching for the number = %d...\n", pid
);
32 /* Initialize the mutex lock */
33 pthread_mutex_init(&lock
, NULL
);
35 /* Create the searching threads */
36 for (i
=0; i
<NUM_THREADS
; i
++)
37 pthread_create(&threads
[i
], 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
);
43 printf("It took %d tries to find the number.\n", tries
);
45 /* Exit the program */
49 /* This is the cleanup function that is called
50 when the threads are cancelled */
52 void print_it(void *arg
)
54 int *try = (int *) arg
;
57 /* Get the calling thread's ID */
60 /* Print where the thread was in its search when it was cancelled */
61 printf("Thread %lx was canceled on its %d try.\n", tid
, *try);
64 /* This is the search routine that is executed in each thread */
66 void *search(void *arg
)
72 /* get the calling thread ID */
75 /* use the thread ID to set the seed for the random number generator */
76 /* Since srand and rand are not thread-safe, serialize with lock */
77 pthread_mutex_lock(&lock
);
79 i
= rand() & 0xFFFFFF;
80 pthread_mutex_unlock(&lock
);
83 /* Set the cancellation parameters --
84 - Enable thread cancellation
85 - Defer the action of the cancellation */
87 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, NULL
);
88 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED
, NULL
);
90 /* Push the cleanup routine (print_it) onto the thread
91 cleanup stack. This routine will be called when the
92 thread is cancelled. Also note that the pthread_cleanup_push
93 call must have a matching pthread_cleanup_pop call. The
94 push and pop calls MUST be at the same lexical level
97 /* Pass address of `ntries' since the current value of `ntries' is not
98 the one we want to use in the cleanup function */
100 pthread_cleanup_push(print_it
, (void *)&ntries
);
104 i
= (i
+ 1) & 0xFFFFFF;
107 /* Does the random number match the target number? */
109 /* Try to lock the mutex lock --
110 if locked, check to see if the thread has been cancelled
111 if not locked then continue */
112 while (pthread_mutex_trylock(&lock
) == EBUSY
)
113 pthread_testcancel();
115 /* Set the global variable for the number of tries */
117 printf("Thread %lx found the number!\n", tid
);
119 /* Cancel all the other threads */
120 for (j
=0; j
<NUM_THREADS
; j
++)
121 if (threads
[j
] != tid
) pthread_cancel(threads
[j
]);
123 /* Break out of the while loop */
127 /* Every 100 tries check to see if the thread has been cancelled. */
128 if (ntries
% 100 == 0) {
129 pthread_testcancel();
133 /* The only way we can get here is when the thread breaks out
134 of the while loop. In this case the thread that makes it here
135 has found the number we are looking for and does not need to run
136 the thread cleanup function. This is why the pthread_cleanup_pop
137 function is called with a 0 argument; this will pop the cleanup
138 function off the stack without executing it */
140 pthread_cleanup_pop(0);