Sign in Join

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

Mailing lists / fpc-pascal / 2010-12

Current TThread

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Mattias Gaertner

Hi,

How to get the current TThread instance?

For example: I created a TThread and called a function, which calls a function .... One of those sub functions needs to call TThread.Synchronize, which needs as parameter the current TThread.

Mattias

Reply by email

Unknown author
to Mattias Gaertner
Mattias Gaertner wrote
Hi, How to get the current TThread instance?

There is no documented method.

For example: I created a TThread and called a function, which calls a function .... One of those sub functions needs to call TThread.Synchronize, which needs as parameter the current TThread.

Pass on the TThread instance.

You could use GetCurrentThreadID and a lookup list.

Michael.

Reply by email

Mattias Gaertner
to Unknown author
Show quoted text 9 lines
On Tue, 21 Dec 2010, Mattias Gaertner wrote: > Hi, > > How to get the current TThread instance? There is no documented method.

Strange.
Am I really the first needing this?

Show quoted text 5 lines
> For example: I created a TThread and called a function, which calls a > function .... One of those sub functions needs to call > TThread.Synchronize, which needs as parameter the current TThread. Pass on the TThread instance.

You mean as parameter?
Some functions are predefined events. That means I call them and they call me back. I would like to avoid adding everywhere another parameter.

You could use GetCurrentThreadID and a lookup list.

What about using a

threadvar CurrentThread: Thread;

?
TThread could set this in the constructor.

BTW, you can call Synchronize for another thread than the current. Is that allowed?

Mattias

Reply by email

Unknown author
to Mattias Gaertner
Mattias Gaertner wrote 15 lines
On Tue, 21 Dec 2010 14:29:36 +0100 (CET) wrote: > > > On Tue, 21 Dec 2010, Mattias Gaertner wrote: > >> Hi, >> >> How to get the current TThread instance? > > There is no documented method. Strange. Am I really the first needing this?

Apparently.
I never needed such a method, and I use threads extensively :-)

Show quoted text 9 lines
>> For example: I created a TThread and called a function, which calls a >> function .... One of those sub functions needs to call >> TThread.Synchronize, which needs as parameter the current TThread. > > Pass on the TThread instance. You mean as parameter?

Yes.

Some functions are predefined events. That means I call them and they call me back. I would like to avoid adding everywhere another parameter.

I understand that. Well, you could create an event object for the event handler.

Show quoted text 9 lines
> You could use GetCurrentThreadID and a lookup list. What about using a threadvar CurrentThread: Thread; ? TThread could set this in the constructor.

This could be done, but will give errors for externally created threads.

But you can easily do this for your own thread objects.

BTW, you can call Synchronize for another thread than the current. Is that allowed?

It's a method like any other. Why not ?

Michael.

Reply by email

Jonas Maebe
to Unknown author
Show quoted text 11 lines
On Tue, 21 Dec 2010, Mattias Gaertner wrote: > What about using a > > threadvar CurrentThread: Thread; > > ? > TThread could set this in the constructor. This could be done, but will give errors for externally created threads.

And for threads created with BeginThread.

But you can easily do this for your own thread objects.

I also think that's the best solution. If that's not enough, then doing it in tthread won't be enough either (unless you only support applications that exclusively use tthread-based threads).

Jonas

Reply by email

Helmut Hartl
to Mattias Gaertner: On Tue, 21 Dec 2010 14:29:36 +0100 (CET) Strange. Am I really the first needing this? You…
Mattias Gaertner wrote 11 lines
On Tue, 21 Dec 2010 14:29:36 +0100 (CET) wrote: > You could use GetCurrentThreadID and a lookup list. What about using a threadvar CurrentThread: Thread; ? TThread could set this in the constructor.

We currently use this method for our "in Application" scheduler, but IIRC you have to fetch/set the ID in the Execute method. But having the overhead of using threadvars (mentionent on the list some time ago) I am currently looking for another method too.

helmut

Reply by email