[RFE] Soft-cancel cThread
Commit Message
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
@@ -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.
@@ -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;
+ }
}
}