On Tue, May 30, 2006 at 07:43:01PM -0700, Luo Kai wrote:
> 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);

No.  Do not do this.  Bad idea.  You don't want asynchronous cancellation if
you are calling any non-async-cancel-safe function.  pthread_mutex_lock()
is a non-async-cancel-safe function.  So is pthread_cond_wait().  In fact,
about the only async-cancel-safe function is pthread_setcanceltype().

> pthread_mutex_lock(&mutex);
> pthread_cond_wait(&cond, &mutex);
> pthread_mutex_unlock(&mutex);
> return NULL;
> }

You need to have a pthread_cleanup_push(pthread_mutex_unlock, &mutex) before
the cond_wait:

        pthread_mutex_lock(&mutex);
        pthread_cleanup_push(pthread_mutex_unlock, &mutex)
        pthread_cond_wait(&cond, &mutex);
        pthread_cleanup_pop(1);         /* unlock the mutex */
        return (NULL);
}

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

Again, you *really* don't want to do this.

Cheers,
- jonathan

-- 
Jonathan Adams, Solaris Kernel Development
_______________________________________________
opensolaris-discuss mailing list
[email protected]

Reply via email to