From patchwork Sun Sep 24 10:08:53 2006 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Klaus Schmidinger X-Patchwork-Id: 12388 Received: from raven.cadsoft.de ([217.7.101.211]) by www.linuxtv.org with esmtp (Exim 4.50) id 1GRQuv-0007g9-LL for vdr@linuxtv.org; Sun, 24 Sep 2006 12:08:57 +0200 Received: from [192.168.100.10] (hawk.cadsoft.de [192.168.100.10]) by raven.cadsoft.de (8.13.3/8.13.3) with ESMTP id k8OA8uwL032114 for ; Sun, 24 Sep 2006 12:08:56 +0200 Message-ID: <45165935.3030709@cadsoft.de> Date: Sun, 24 Sep 2006 12:08:53 +0200 From: Klaus Schmidinger Organization: CadSoft Computer GmbH User-Agent: Thunderbird 1.5.0.4 (X11/20060516) MIME-Version: 1.0 To: vdr@linuxtv.org Subject: Re: [vdr] [RFE] Soft-cancel cThread References: <45156F49.2070409@gmx.de> In-Reply-To: <45156F49.2070409@gmx.de> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0 (raven.cadsoft.de [192.168.1.1]); Sun, 24 Sep 2006 12:08:56 +0200 (CEST) X-BeenThere: vdr@linuxtv.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: VDR Mailing List List-Id: VDR Mailing List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 Sep 2006 10:08:57 -0000 Status: O X-Status: X-Keywords: X-UID: 10745 Udo Richter wrote: > Hi, > > I have a suggestion for a VDR change: I could use a way to end a cThread > externally without having to wait and without hard canceling it. > > My thread runs with the usual while (Running()) loop. To signal the loop > to stop, it would be enough to set running=false. However, running is a > private member of cThread. The only way to set running=false is to call > Cancel(). This involves killing the thread and waiting for the thread to > terminate. Two things I actually don't want. > > There are two ways this could be done. First, by adding a function > SoftCancel() { running=false; }. Or second, by modifying Cancel() to > just set running=false if called with Cancel(-1) or Cancel(0,false) or > similar. I would prefer using -1 as a special value for this, because this wouldn't require an interface change. Does the attached patch do what you want? Klaus --- thread.h 2006/01/08 11:40:23 1.36 +++ thread.h 2006/09/24 10:05:41 @@ -103,6 +103,8 @@ ///< the Action() loop can finish in an orderly fashion and then waiting ///< up to WaitSeconds seconds for the thread to actually end. If the ///< thread doesn't end by itself, it is killed. + ///< If WaitSeconds is -1, only 'running' is set to false and Cancel() + ///< returns imemdiately, without killing the thread. public: cThread(const char *Description = NULL); ///< Creates a new thread. --- thread.c 2006/08/20 10:20:44 1.57 +++ thread.c 2006/09/24 10:04:21 @@ -302,9 +302,11 @@ } esyslog("ERROR: %s thread %d won't end (waited %d seconds) - canceling it...", description ? description : "", childThreadId, WaitSeconds); } - pthread_cancel(childTid); - childTid = 0; - active = false; + if (WaitSeconds > -1) { + pthread_cancel(childTid); + childTid = 0; + active = false; + } } }