From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30496 invoked by alias); 29 Sep 2005 17:10:58 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 30480 invoked by uid 22791); 29 Sep 2005 17:10:54 -0000 Received: from a.mail.sonic.net (HELO a.mail.sonic.net) (64.142.16.245) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Thu, 29 Sep 2005 17:10:54 +0000 Received: from [10.8.221.115] (209-204-163-8.vpn.sonic.net [209.204.163.8]) (authenticated bits=0) by a.mail.sonic.net (8.13.3/8.13.3) with ESMTP id j8THAmti022222 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO); Thu, 29 Sep 2005 10:10:48 -0700 Message-ID: <433C2017.3040504@hawksoft.com> Date: Thu, 29 Sep 2005 17:10:00 -0000 From: "Phil Frisbie, Jr." User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) MIME-Version: 1.0 To: limin.fu@ircc.it CC: pthreads-win32@sources.redhat.com Subject: Re: A question about thread cancellation References: <32883.130.192.136.198.1128010379.squirrel@130.192.136.198> In-Reply-To: <32883.130.192.136.198.1128010379.squirrel@130.192.136.198> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-SW-Source: 2005/txt/msg00124.txt.bz2 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