1 /** Cancel a thread that holds a lock on a mutex. */
10 pthread_mutex_t s_mutex1
;
11 pthread_mutex_t s_mutex2
;
14 static void* thread(void* arg
)
17 pthread_mutex_lock(&s_mutex2
);
18 /* Inform the main thread that s_mutex2 has been locked, and wait for pthread_cancel(). */
19 pthread_mutex_lock(&s_mutex1
);
20 pthread_cond_signal(&s_cond
);
21 pthread_cond_wait(&s_cond
, &s_mutex1
);
25 int main(int argc
, char** argv
)
29 /* Initialize synchronization objects. */
30 pthread_cond_init(&s_cond
, 0);
31 pthread_mutex_init(&s_mutex1
, 0);
32 pthread_mutex_init(&s_mutex2
, 0);
35 pthread_mutex_lock(&s_mutex1
);
36 pthread_create(&tid
, 0, &thread
, 0);
38 /* Wait until the created thread has locked s_mutex2. */
39 pthread_cond_wait(&s_cond
, &s_mutex1
);
40 pthread_mutex_unlock(&s_mutex1
);
42 /* Cancel the created thread. */
45 /* Join the created thread. */
48 /* Invoke pthread_cancel() with an invalid thread ID. */
51 fprintf(stderr
, "Test finished.\n");