This is the mail archive of the
mailing list for the pthreas-win32 project.
Re: problem using pthread_cancel and pthread_mutex_lock
- From: "vc" <vcotirlea1 at hotmail dot com>
- To: "Simon Gerblich" <sgerblich at daronmont dot com dot au>
- Cc: <pthreads-win32 at sources dot redhat dot com>
- Date: Thu, 26 Feb 2004 17:00:54 +0100
- Subject: Re: problem using pthread_cancel and pthread_mutex_lock
- References: <8179ED123ECCD611A5490000F822E6EA3825E4@mail.daronmont.com.au>
Thanks to all of you for your answers.
----- Original Message -----
From: "Simon Gerblich" <email@example.com>
To: "vc" <firstname.lastname@example.org>
Sent: Tuesday, February 24, 2004 5:23 AM
Subject: RE: problem using pthread_cancel and pthread_mutex_lock
> Hi Viv,
> >So, my question is: how can a thread cleanly cancel another thread
> >which is waiting in a 'pthread_mutex_lock' call, so that this mutex is
> >available again ?
> I can see a few problems in what you are doing.
> A mutex is not really designed to be locked in a thread for a long time
> unlocked by another thread. When I use a mutex in a thread I lock it - do
> what I have to do - and then unlock it ASAP. I don't use mutexes to
> synchronise threads. I use them to protect data. Pthread mutexes are not
> cancellation points.
> I use deferred cancellation instead of asynchronous cancellation because
> recommendations in "Programming with POSIX Threads" by David Butenhof.
> Page 150 "Avoid asynchronous cancellation. It is difficult to use
> and is rarely useful."
> Page 151 "Asynchronous cancellation can occur at any hardware instruction.
> On some computers it may even be possible to interrupt some instructions
> the middle. That makes it really difficult to determine what the canceled
> thread was doing."
> Page 151 "Call no code with asynchronous cancellation enabled unless you
> wrote it to be async-cancel safe - and even then, think twice!"
> I write my applications with deferred cancellation and the threads
> on message queues, which are cancellation points. When a cancel is
> requested the threads exit cleanly when they next go to read the message
> queue. I have minimal cancellation points in my code so that I know
> where my threads will cancel.
> Without knowing exactly what you are trying to do I think you should have
> look at using a condition variable instead of a mutex for your thread
> syncing. Waiting on a condition variable is a cancellation point.
> Be aware that the Sleep() function on windows is not a cancellation point.
> I have written my own sleep functions to use with pthreads-win32 that
> on condition variables with timeouts.
> If your not lurking in the google groups comp.programming.threads I'd
> recommend it. I've learnt a lot following threads on pthreads, etc.