Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 8 of 8 total
Thread OT: threadvar instead var
Tue, Oct 16 2012 8:46 AMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Hello

I have a situation I need to use a unit with global vars. When this unit is
used for thread access I use threadvar instead.

My doubt is, could I use threadvar instead of var in regular applications ?
IOW, in this special case if I only have one version of this unit with
threadvar it could create a problem or slowdown of something ?

Eduardo

Tue, Oct 16 2012 12:09 PMPermanent Link

Raul

Globestar Systems

Team Elevate Team Elevate

Eduardo,

Sorry but i'm bit confused about what you wish to achieve?

The concept of threadvar and global var are at odds with each other in
my opinion. If you use the threadvar then when do you populate it and
how do you deal with the different values in all threads ?

There is nothing wrong using global vars from threads as long as you
control access to (critical section for example) writing of the value.
Reading generally is thread-safe.

Raul



On 10/16/2012 8:46 AM, Eduardo [HPro] wrote:
> Hello
>
> I have a situation I need to use a unit with global vars. When this unit is
> used for thread access I use threadvar instead.
>
> My doubt is, could I use threadvar instead of var in regular applications ?
> IOW, in this special case if I only have one version of this unit with
> threadvar it could create a problem or slowdown of something ?
>
> Eduardo
>
>
Tue, Oct 16 2012 1:44 PMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Raul

Let me rephrase the question.

Is there any side effect situation if I use "threadvar" in an application
with just one thread, instead of using "var" ?

I know when use "var" in a multi-thread, it is a complete different
situation when use "threadvar". But thinking just with one thread is my
doubt with issues like performance slow down of other side effect.

Eduardo

Tue, Oct 16 2012 2:38 PMPermanent Link

Raul

Globestar Systems

Team Elevate Team Elevate


AFAIK the threadvar in delphi uses the thread local storage (TLS) to
actually store the data so yes there would be some impact on performance.

I have not seen any hard numbers though but if wiki
(http://en.wikipedia.org/wiki/Thread-local_storage) is to be believed
then for windows the thread info block has the TLS address so you might
be incurring an an additional lookup instruction to get to data but
that's all.

Hence there might be a small performance impact especially if you
reference the variable a lot but most likely you won't notice it.

The other things to keep in mind is that dynamic variables are not
references counted so if you store strings for example then you need to
clear them yourself or you will leak memory.

Raul



On 10/16/2012 1:44 PM, Eduardo [HPro] wrote:
> Raul
>
> Let me rephrase the question.
>
> Is there any side effect situation if I use "threadvar" in an application
> with just one thread, instead of using "var" ?
>
> I know when use "var" in a multi-thread, it is a complete different
> situation when use "threadvar". But thinking just with one thread is my
> doubt with issues like performance slow down of other side effect.
>
> Eduardo
>
>
Wed, Oct 17 2012 6:16 AMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Raul

Thanks for all explanations.
This is exactly what I want (and suspect).

Eduardo

Thu, Oct 25 2012 9:41 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Eduardo,

<< My doubt is, could I use threadvar instead of var in regular applications
?  >>

The question is: why would you want to ?  IOW, what are you hoping to
achieve, simply a uniformity in the code ?

In general, I avoid threadvar like the plague. It's kludgy and its effects
can normally be accomplished in a different, cleaner way.

Tim Young
Elevate Software
www.elevatesoft.com
Thu, Oct 25 2012 2:58 PMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Tim

> The question is: why would you want to ?  IOW, what are you hoping to
> achieve, simply a uniformity in the code ?
Yes, that is the point.

> In general, I avoid threadvar like the plague. It's kludgy and its effects
> can normally be accomplished in a different, cleaner way.
Me too, but in this specific case I need it but I have solved my
trouble/situation using compiler directives.

{$ifdef REMOTO} threadvar {$else} var {$endif} aRef: array[0..200,0..1] of
Integer;

Eduardo

Wed, Nov 14 2012 11:23 AMPermanent Link

Matthew Jones

> The other things to keep in mind is that dynamic variables are not
> references counted so if you store strings for example then you
> need to clear them yourself or you will leak memory.

This is what I was going to mention. It isn't hard, but you need to make sure your
thread tidies up "globals" in threadvars. Use carefully.

I use a threadvar for the variable that I keep my data module object that I put my
DBISAM components on. It means I can have general libraries do their own sessions
safely, but you do need to be sure what you are doing.

/Matthew Jones/
Image