Board index » cppbuilder » Info sought on thread local storage (TLS)

Info sought on thread local storage (TLS)


2005-06-16 06:27:41 AM
cppbuilder52
Hello out there,
while I am familiar with writing multi-threaded apps, the concept of
thread local storage (TLS) is rather unknown to me.
Now it appears that the time has come where I will have to use this
feature, so I am looking for some sort of a general overview on TLS
(hopefully providing some examples).
Any links will be greatly appreciated.
Best regards
Helmut Giese
 
 

Re:Info sought on thread local storage (TLS)

"Helmut Giese" < XXXX@XXXXX.COM >wrote in message
Quote
while I am familiar with writing multi-threaded apps, the concept
of thread local storage (TLS) is rather unknown to me.
Basically, an API thread object has an array available where applications
can store their own values to at runtime. For the most part, you don't need
to worry about working with TLS, especially if you have classes wrapping
thread objects for you, such as the VCL's TThread class.
Quote
Now it appears that the time has come where I will have to use this
feature, so I am looking for some sort of a general overview on TLS
(hopefully providing some examples).
The Win32 API documentation has a chapter on TLS.
Gambit
 

Re:Info sought on thread local storage (TLS)

Warning:
Thread Local Storage, TLS, provides a very small amount of memory. Every
compiler supplies an alternate function to that provided by Win32 for
starting a thread. The compiler-supplied function(s) set up a data block
and place the address of that block and possibly some flags into the TLS
memory.
If you alter TLS memory you may crash the program and possibly crash the
machine. If you Win32 function to start the thread in a C/C++ program then
you also risk causing a crash.
. Ed
Quote
Helmut Giese wrote in message
news: XXXX@XXXXX.COM ...

Hello out there,
while I am familiar with writing multi-threaded apps, the concept of
thread local storage (TLS) is rather unknown to me.
Now it appears that the time has come where I will have to use this
feature, so I am looking for some sort of a general overview on TLS
(hopefully providing some examples).
Any links will be greatly appreciated.
 

{smallsort}

Re:Info sought on thread local storage (TLS)

Hi Ed,
Quote
Warning:
early warnings are always appreciated - might save a lot of time later
on.
Quote

Thread Local Storage, TLS, provides a very small amount of memory. Every
compiler supplies an alternate function to that provided by Win32 for
starting a thread. The compiler-supplied function(s) set up a data block
and place the address of that block and possibly some flags into the TLS
memory.

If you alter TLS memory you may crash the program and possibly crash the
machine. If you Win32 function to start the thread in a C/C++ program then
you also risk causing a crash.
Huh? Doesn't sound exactly promising.
Hm, I think I will do some reading and prolong the design phase, maybe
things can be simplified.
Thanks for the alert and best regards
Helmut Giese
 

Re:Info sought on thread local storage (TLS)

Hi Gambit,
Quote
Basically, an API thread object has an array available where applications
can store their own values to at runtime. For the most part, you don't need
to worry about working with TLS, especially if you have classes wrapping
thread objects for you, such as the VCL's TThread class.

>Now it appears that the time has come where I will have to use this
>feature, so I am looking for some sort of a general overview on TLS
>(hopefully providing some examples).

The Win32 API documentation has a chapter on TLS.
Thanks, I'll go and have a look.
Best regards
Helmut Giese
 

Re:Info sought on thread local storage (TLS)

Ed Mulroy wrote:
Quote
Warning:

Thread Local Storage, TLS, provides a very small amount of memory. Every
compiler supplies an alternate function to that provided by Win32 for
starting a thread. The compiler-supplied function(s) set up a data block
and place the address of that block and possibly some flags into the TLS
memory.

If you alter TLS memory you may crash the program and possibly crash the
machine.
Um, yeah, if you go in and trash the place, sure.. but the TLS example
code shows the thread doing LocalAlloc (which can be any size) and
placing the pointer into TLS. So, you don't actually mess with
TLStorage, just the LocalAlloc buffer.
Only advantage I see is the thread doesn't have to pass the pointer to
all of it's subroutines.
Oh, perhaps you were warning that VCL and perhaps TThread already use
that slot, and overwritting it would be problematic? So, one should
ensure TlsGetValue() == 0 before TlsSetValue(), at least for testing
purposes.
Quote
If you Win32 function to start the thread in a C/C++ program then
you also risk causing a crash.
That one didn't parse?
CreateThread()? Crashing?
 

Re:Info sought on thread local storage (TLS)

"Helmut Giese" < XXXX@XXXXX.COM >wrote in message
Quote
Huh? Doesn't sound exactly promising.
What exactly are you trying to accomplish in the first place? Typically,
wrapping the thread in its own class is much more flexible than using TLS.
Then you can put whatever you want into the class. TLS is typiucally only
used by the OS for its own internal uses (storing COM thread concurrency
flags, GetLastError() error codes, etc).
Gambit
 

Re:Info sought on thread local storage (TLS)

Quote
What exactly are you trying to accomplish in the first place? Typically,
wrapping the thread in its own class is much more flexible than using TLS.
Then you can put whatever you want into the class. TLS is typiucally only
used by the OS for its own internal uses (storing COM thread concurrency
flags, GetLastError() error codes, etc).
Hi Gambit,
I am at the design phase of simulating a microcontroller. One thread
will handle the main program while each interrupt service routine will
get its own thread, so that they can interrupt each other "like in the
real world".
The whole concept is still somewhat misty and at one point in time I
thought I might need TLS - or rather learn enough about TLS to be able
to judge whether it would be helpful here.
The problematic part for the moment is that all these threads will do
some reporting (we want to know what's going one) and this is the
bottleneck right now: they can (and will) interrupt each other there
and I want a robust design which can be carried over to "Version 2"
(when we add simulating an RTOS - Real Time OS - on top of this).
We'll just leave it at this point for the moment. I'm at the drawing
board drawing little pictures ...
Best regards
Helmut Giese