public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* A question about thread cancellation
@ 2005-09-29 16:13 Fu Limin
  2005-09-29 17:10 ` Phil Frisbie, Jr.
  0 siblings, 1 reply; 2+ messages in thread
From: Fu Limin @ 2005-09-29 16:13 UTC (permalink / raw)
  To: pthreads-win32

Hello,

I have a question on thread cancellation, which is not specifically
relevant to pthread-win32, I hope you will not mind if I post it in this
mailling list.

In my program, there is a global mutex and a condition variable. And there
are several threads wait on this cond.var. under the protection of this
mutex. When I cancel one of the threads, I found there is a trouble in the
thread clean up. Because the mutex can be in either locked or unlocked
state. And if it is in locked state, I can't tell if it is locked by
another thread, or it gets the locked state from the cancellation point
pthread_cond_wait()! If it is locked by another thread, I should do
nothing on it, but if it gets the locked state by returning from
pthread_cond_wait() due to cancellation, I should unlock it in the clean
up.

So the problem is, how can I know, in the clean up, when should I unlock
the mutex, and when not? Did any body have experienced similar problem?
Does anybody have any hints on this?

Thanks a lot!!!

Limin

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: A question about thread cancellation
  2005-09-29 16:13 A question about thread cancellation Fu Limin
@ 2005-09-29 17:10 ` Phil Frisbie, Jr.
  0 siblings, 0 replies; 2+ messages in thread
From: Phil Frisbie, Jr. @ 2005-09-29 17:10 UTC (permalink / raw)
  To: limin.fu; +Cc: pthreads-win32

Fu Limin wrote:

> Hello,
> 
> I have a question on thread cancellation, which is not specifically
> relevant to pthread-win32, I hope you will not mind if I post it in this
> mailling list.
> 
> In my program, there is a global mutex and a condition variable. And there
> are several threads wait on this cond.var. under the protection of this
> mutex. When I cancel one of the threads, I found there is a trouble in the
> thread clean up. Because the mutex can be in either locked or unlocked
> state. And if it is in locked state, I can't tell if it is locked by
> another thread, or it gets the locked state from the cancellation point
> pthread_cond_wait()! If it is locked by another thread, I should do
> nothing on it, but if it gets the locked state by returning from
> pthread_cond_wait() due to cancellation, I should unlock it in the clean
> up.
> 
> So the problem is, how can I know, in the clean up, when should I unlock
> the mutex, and when not? Did any body have experienced similar problem?
> Does anybody have any hints on this?

In my opinion it is EVIL (or at least bad programming) to cancel threads. It is 
better to architect your code so that you can request a thread to exit on it's own.

-- 
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-09-29 17:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-29 16:13 A question about thread cancellation Fu Limin
2005-09-29 17:10 ` Phil Frisbie, Jr.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).