Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: System.Threading TThreadPool and TLS



Permlink Replies: 3 - Last Post: Dec 18, 2017 12:56 AM Last Post By: Matthew Jones Threads: [ Previous | Next ]
Roberto Della P...

Posts: 83
Registered: 4/8/12
System.Threading TThreadPool and TLS
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 2, 2017 1:57 AM
hello,
I'm using the very powerful TTASK api of System.Threading,
however I need a thing:
the pool is dynamic, I attach a TLS class with DB components into the running thread of the context, and keeping it in memory until the thread runs (avoiding create and destroy every time a db context).
I need to have an event OnAThreadExit of the System.TThreadPool so I can free the TLS class when the thread is made free.
How can be done without change the source of System.Threading.pas? Winapi doesn't offer a callback on thread exit, only a poll api is found, so cannot be used (I need to call TLS code within the thread, before the exit).

thank you
R.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: System.Threading TThreadPool and TLS
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 4, 2017 11:04 AM   in response to: Roberto Della P... in response to: Roberto Della P...
Roberto Della Pasqua wrote:

I need to have an event OnAThreadExit of the System.TThreadPool so I
can free the TLS class when the thread is made free. How can be done
without change the source of System.Threading.pas?

Unfortunately, TThreadPool does not support what you are asking for.
It does not give you access to its worker threads so you can replace
them with your own thread class, or otherwise customize their run
behaviors. You will have to modify TThreadPool's source code do
achieve what you want.

Winapi doesn't offer a callback on thread exit

That is not entirely true. Any DLL that is loaded into your process
will have its entry point function called whenever a thread starts and
exits (unless the Win32 API DisableThreadLibraryCalls() function is
called for the DLL, that is). So, you could create your own DLL and
have it assign an event handler to Delphi's SysInit.DLLProc/Ex callback
to catch DLL_THREAD_DETACH events, and then load that DLL at runtime
before starting your thread pool.

I need to call TLS code within the thread, before the exit

If you use Delphi-style TLS (ie, the 'threadvar' keyword) then the RTL
already handles cleaning up TLS memory on exit of Delphi-style threads.
But, if you are storing custom data in your own TLS memory, then yes,
you need to clean it up manually.

Since TThreadPool uses TThread, you could try assigning an event
handler to the System.SystemThreadEndProc (Windows) or
System.EndThreadProc (Posix) callback, which gets called whenever a
TThread exits. Then you wouldn't need a separate DLL.

--
Remy Lebeau (TeamB)
Thorsten Engler

Posts: 41
Registered: 6/25/98
Re: System.Threading TThreadPool and TLS
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2017 3:33 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

If you use Delphi-style TLS (ie, the 'threadvar' keyword) then the RTL
already handles cleaning up TLS memory on exit of Delphi-style
threads.
That, is unfortunately not true.

Last I checked, the Delphi RTL doesn't actually finalize any threadvars
of managed types when a thread exits (no matter if it's a delphi
TThread or an external thread).

So if you should store strings, dynamic arrays, variants, or interfaces
(or records containing any of these) in a threadvar, they are all being
leaked on thread exit without being finalized.
Matthew Jones

Posts: 337
Registered: 1/25/98
Re: System.Threading TThreadPool and TLS
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 18, 2017 12:56 AM   in response to: Thorsten Engler in response to: Thorsten Engler
Thorsten Engler wrote:

Last I checked, the Delphi RTL doesn't actually finalize any threadvars
of managed types when a thread exits (no matter if it's a delphi
TThread or an external thread).

I agree. You can manually finalise these items (I do this with a string I keep), but you have to be very explicit. If your system will run forever, you'd need to do this to stop leaks.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02