Update.
[glibc.git] / linuxthreads / Examples / ex3.c
blob7557cc79838ac0b15b2677f2abf5459a121505f6
1 /* Multi-thread searching.
2 Illustrates: thread cancellation, cleanup handlers. */
4 #include <errno.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <pthread.h>
11 /* Defines the number of searching threads */
12 #define NUM_THREADS 5
14 /* Function prototypes */
15 void *search(void *);
16 void print_it(void *);
18 /* Global variables */
19 pthread_t threads[NUM_THREADS];
20 pthread_mutex_t lock;
21 int tries;
23 int main(int argc, char ** argv)
25 int i;
26 int pid;
28 /* create a number to search for */
29 pid = getpid();
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 */
46 return 0;
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;
55 pthread_t tid;
57 /* Get the calling thread's ID */
58 tid = pthread_self();
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)
68 int num = (int) arg;
69 int i, j, ntries;
70 pthread_t tid;
72 /* get the calling thread ID */
73 tid = pthread_self();
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);
78 srand((int)tid);
79 i = rand() & 0xFFFFFF;
80 pthread_mutex_unlock(&lock);
81 ntries = 0;
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
95 within the code */
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);
102 /* Loop forever */
103 while (1) {
104 i = (i + 1) & 0xFFFFFF;
105 ntries++;
107 /* Does the random number match the target number? */
108 if (num == i) {
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 */
116 tries = ntries;
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 */
124 break;
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);
141 return((void *)0);