Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-05

TThread and WaitFor

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

LacaK

Hi *,
is this safe:

FThread.Terminate; // what if here is switched FThread to execution and
will immediately end his Execute procedure
FThread.WaitFor; // what happens if FThread is freed before WaitFor is
executed

?

assuming, that FThread has FreeOnTerminate := True; (looking into source code it can IMO happen, that ThreadProc is ended and data allocated to FThread instance are freed thanks to FreeOnTerminate)

Is there any technique, when I need wait for thread to end execution and still have FreeOnTerminate = True ?

Thanks
-Laco.

Reply by email

Sven Barth
to LacaK
"LacaK" wrote 5 lines
Hi *, is this safe: FThread.Terminate; // what if here is switched FThread to execution and

will immediately end his Execute procedure

FThread.WaitFor; // what happens if FThread is freed before WaitFor is

executed

Show quoted text 5 lines
? assuming, that FThread has FreeOnTerminate := True; (looking into source code it can IMO happen, that ThreadProc is ended and

data allocated to FThread instance are freed thanks to FreeOnTerminate)

Is there any technique, when I need wait for thread to end execution and

still have FreeOnTerminate = True ?

Either don't use FreeOnTerminate in that case or use an event that is owned outside of the thread and that is triggered before exiting the thread's Execute method.

Regards,
Sven

Reply by email