[fpc-pascal] TThread.FreeOnTerminate

Luca Olivetti luca at ventoso.org
Fri Dec 14 19:56:51 CET 2018

El 14/12/18 a les 18:08, Jonas Maebe ha escrit:
> On 14/12/18 11:09, Luca Olivetti wrote:
>> I think that TThread should have a method to kill it for such cases, 
>> using, e.g., pthread_cancel under unix
> pthread_cancel() does not not kill a thread. Conceptually, it is exactly 
> the same as calling TThread.terminate: it just sets a property for the 
> thread to indicate someone wants it to stop. The thread will only stop 
> the next time pthread_testcancel() gets subseaquently called from inside 
> this "cancelled" thread, which should only be done at well-defined 
> points so the chances at leaving stray locks etc behind are minimalised.

Oh, I'm not familiar with those APIs (that's why I use freepascal ;-), I 
just looked at the implementation of a thread class in vdr 
(http://www.tvdr.de/) documented this way:

   void Cancel(int WaitSeconds = 0);
        ///< Cancels the thread by first setting 'running' to false, so that
        ///< the Action() loop can finish in an orderly fashion and then 
        ///< 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 
        ///< returns immediately, without killing the thread.

and the method uses pthread_cancel. So the documentation of that class 
is misleading.
I also read the manual page for pthread_cancel and even when using 
pthread_canceltype(PTHREAD_CANCEL_AYNCHRONOUS) "The thread can be 
canceled at any time.  (Typically, it will be canceled immediately upon 
receiving a cancellation request, but the system doesn't guarantee this.)".


More information about the fpc-pascal mailing list