Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

pthread_cancel() does NOT work with several threads

807578May 30 2006 — edited May 31 2006
See the following code:

------------test.c--------
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/resource.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void func(void a)
{
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
return NULL;

}
int main()
{
int ii;
pthread_t tid[3];
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

for(ii = 0;ii < 3;ii++)
{
pthread_create(&tid[ii], NULL, func, NULL);
printf("tid = %d\n", tid[ii]);
}
sleep(2);

for(ii = 0;ii < 3;ii++)
{
printf("cancel = %d\n", pthread_cancel(tid[ii]));
}

for(ii = 0;ii < 3;ii++)
{
printf("join = %d\n", pthread_join(tid[ii], NULL));
}
}
---------------test.c--------

$CC -o test test.c -lpthread
$./test
tid = 2
tid = 3
tid = 4
cancel = 0
cancel = 0
cancel = 0
join = 0

--------------------

main thread is blocked by pthread_join() for the thread with thread id 3.
I am wondering why pthread_cancel() can successfully cancel the thread with thread id 2, but it can not cancel the threads with thread id 3 and 4. Threads 2, 3, 4 all were being blocked on pthread_cond_wait() before being cancelled, and pthread_cond_wait() is a cancellation point.

Message was edited by:
KaiLuo

Message was edited by:
KaiLuo
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 28 2006
Added on May 30 2006
6 comments
410 views