Sign in Join

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

Mailing lists / fpc-pascal / 2008-09

How to initialize thread vars?

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Vinzent Höfler

Hi,

suppose I need some threadvars inside dynamically allocated objects to keep track of the number of calls to certain methods. Basic requirement is to keep track of recursive calls per thread, so I tried implementing that with thread vars. Only problem: How do I initialize them? I can't do it from within the constructor, because it most probably runs in a different context. I can't do it from within the object itself, because any flag indicating that the thread vars are initialized would itself be required to be a thread var...

The only "clean" solution to me seems to hook the BeginThread routine of the thread manager, but I don't like that approach too much...

Perhaps someone has a smarter idea?

Regards,

Vinzent.

Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
Tobias Giesen
to Vinzent Höfler

Hi,

I think that thread variables are automatically initialized to zero. So you just need to use them in such a way that zero is the desired start value.

Cheers,
Tobias

Vinzent Höfler
to Tobias Giesen

Hi,

I think that thread variables are automatically initialized to zero. So you just need to use them in such a way that zero is the desired start value.

Hmm, yes. I just checked the sourcecode and indeed, for Win32 and Unix at least the threadvars are indeed initialized to zero. That would fit nicely here. ;)

If any of the developers could confirm, whether this is guaranteed semantics or was just a sane choice by the implementor? Because the documentation reads quite a bit differently:

<http://www.freepascal.org/docs-html/ref/refse22.html#x53-590004.4> states:

"Note that the copy is made with the original value of the variable, not with the value of the variable at the time the thread is started."

Which doesn't make too much sense to me considering that I can't initialize thread vars at all (compiler complains: 'Cannot initialize variables declared as threadvar'). So what would this "original value" be (besides the obvious zero - which is the default value for all global variables)?

Vinzent.

GMX Kostenlose Spiele: Einfach online spielen und Spaß haben mit Pastry Passion! http://games.entertainment.gmx.net/de/entertainment/games/free/puzzle/6169196
Michael Van Canneyt
to Vinzent Höfler
"Vinzent Höfler" wrote 15 lines
Hi, > I think that thread variables are automatically initialized to zero. So > you just need to use them in such a way that zero is the desired start > value. Hmm, yes. I just checked the sourcecode and indeed, for Win32 and Unix at least the threadvars are indeed initialized to zero. That would fit nicely here. ;) If any of the developers could confirm, whether this is guaranteed semantics or was just a sane choice by the implementor? Because the documentation reads quite a bit differently: <http://www.freepascal.org/docs-html/ref/refse22.html#x53-590004.4> states: "Note that the copy is made with the original value of the variable, not with the value of the variable at the time the thread is started." Which doesn't make too much sense to me considering that I can't initialize thread vars at all (compiler complains: 'Cannot initialize variables declared as threadvar'). So what would this "original value" be (besides the obvious zero - which is the default value for all global variables)?

The original value is zero.

Michael.