Board index » delphi » ThreadVar keyword

ThreadVar keyword

Does anybody know what is the correct use of threadvar ?
Thanks

D. Fourr

 

Re:ThreadVar keyword


: "Denis Fourr" <apilo...@wanadoo.fr> wrote:

Quote
>Does anybody know what is the correct use of threadvar ?

Exactly the same as for the "var" keyword - just for thread data.

--
Stefan Hoffmeister    (http://www.econos.de/)
No private email, please, unless expressly invited.

Re:ThreadVar keyword


Quote
"Denis Fourr" <apilo...@wanadoo.fr> wrote:
>Does anybody know what is the correct use of threadvar ?

Are you asking "when would I use it," or "how do I use it"?

"How" was answered by another poster: just like var at the global level.

"When" is if you ever need data which is local to a thread.  "threadvar"
sets up what Microsoft calls "thread local storage".  Ordinarily, a global
variable is shared between all threads in your application: only one piece
of memory is allocated, and all threads refer to the same piece of memory.
With thread local storage, each thread has its own memory, separate from
and inaccessible by the other threads.

In my experience, this is a relatively rare need.  My multithreaded apps
have handled this in two ways: either by allocating a thread context
structure/object in the main thread and passing it to the worker thread, or
by using data on the stack.

- Tim Roberts, t...@probo.com
  Providenza & Boekelheide, Inc.

Re:ThreadVar keyword


Quote
>In my experience, this is a relatively rare need.  My multithreaded apps
>have handled this in two ways: either by allocating a thread context
>structure/object in the main thread and passing it to the worker thread, or
>by using data on the stack.

I found threadvar a very good solution for quick-and-dirty conversions of
old, thread unsafe code into thread safe code. I repeat: quick and dirty. It
is alway beter to rewrite the whole thing.

Primoz

Other Threads