Sign in Join

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

Mailing lists / fpc-pascal / 2018-06

Support for "class threadvar" sections inside class and record types

10 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

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

Sven Barth

Hello together!

Announcing a small feature this time, but some might appreciate it: FPC now supports the declaration of threadvars inside class and record types.

=== code begin ===

{$mode objfpc}
{$modeswitch advancedrecords}
type
   TTestClass = class
   public class threadvar
     Test: LongInt;
   end;
   TTestRecord = record
   public class threadvar
     Test: LongInt;
   end;
begin
   TTestClass.Test := 42;
   TTestRecord.Test := 21;
end.

=== code end ===

A threadvar section is started using "class threadvar" and the "class" specifier is required. They are accessed like ordinary class variables with the difference that they'll hold different values per thread - the same as global threadvars after all.

This feature is Delphi compatible (and was added, because I noticed that Delphi supported it and FPC did not 😅)

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
Show quoted text 5 lines
On Jun 23, 2018, at 8:55 PM, Sven Barth via fpc-pascal wrote: A threadvar section is started using "class threadvar" and the "class" specifier is required. They are accessed like ordinary class variables with the difference that they'll hold different values per thread - the same as global threadvars after all. This feature is Delphi compatible (and was added, because I noticed that Delphi supported it and FPC did not 😅)

That’s an interesting idea I wonder how it could be used. Does it do anything to help with shared data between threads or is just a way to get quick access to thread safe memory?

Regards,
Ryan Joseph

Reply by email

Marco van de Voort
to Ryan Joseph

In our previous episode, Ryan Joseph said:

Show quoted text 8 lines
> > A threadvar section is started using "class threadvar" and the "class" specifier is required. They are accessed like ordinary class variables with the difference that they'll hold different values per thread - the same as global threadvars after all. > > This feature is Delphi compatible (and was added, because I noticed that Delphi supported it and FPC did not ?? That?s an interesting idea I wonder how it could be used. Does it do anything to help with shared data between threads or is just a way to get quick access to thread safe memory?

More probably they needed it for the TMonitor implementation, which is used as a kind of spinlock in front of every OS synchronization primitive.

Reply by email

Sven Barth
to Ryan Joseph: That’s an interesting idea I wonder how it could be used. Does it do anything to help…

Ryan Joseph schrieb am Sa., 23. Juni 2018,
16:04:

Show quoted text 17 lines
> On Jun 23, 2018, at 8:55 PM, Sven Barth via fpc-pascal < > wrote: > > A threadvar section is started using "class threadvar" and the "class" specifier is required. They are accessed like ordinary class variables with the difference that they'll hold different values per thread - the same as global threadvars after all. > > This feature is Delphi compatible (and was added, because I noticed that Delphi supported it and FPC did not 😅) That’s an interesting idea I wonder how it could be used. Does it do anything to help with shared data between threads or is just a way to get quick access to thread safe memory?

It's merely a scoped threadvar, so nothing special there. And in case you don't know about threadvars: the concept of them is that every thread has its own "instance" of the variable.

See also here: https://www.freepascal.org/docs-html/ref/refse26.html

Regards,
Sven

Reply by email

Sven Barth
to Anthony Walter

Anthony Walter schrieb am Sa., 23. Juni 2018, 22:56:

Show quoted text 14 lines
Much thanks Sven! I'm note sure when I'll use it, but the option is always nice to have. A small question, since it's available in a class, does "class threadvar" support generics? {$mode delphi} type TAnimal<T: TTrainer> = class public class threadvar Trainer: T; end; For everyone else, threadvar is the language integrated simplification of thread local storage <https://en.wikipedia.org/wiki/Thread-local_storage>.

In theory yes, but class variables inside generics currently don't work as one expects (they never did) and will need quite an intensive fix.

Regards,
Sven

Reply by email

Sven Barth
to R0b0t1: I found them one day as well and was confused. The note that they implement TMonitor is…

R0b0t1 schrieb am Sa., 23. Juni 2018, 18:50:

Show quoted text 18 lines
On Saturday, June 23, 2018, Ryan Joseph wrote: > > >> On Jun 23, 2018, at 9:45 PM, Sven Barth via fpc-pascal < > wrote: >> >> See also here: https://www.freepascal.org/docs-html/ref/refse26.html > > I had no idea this existed. When was it added? > > Regards, > Ryan Joseph > I found them one day as well and was confused. The note that they implement TMonitor is helpful. I know of no other uses.

The TMonitor was a guess by Michael.
Threadvars themselves have many uses: the per thread heap management that FPC's heap uses, one output file per thread, so that StdOut doesn't cross, the current TThread instance using TThread.CurrentThread (which Delphi probably implemented using a private class threadvar while for FPC I used a global threadvar in the implementation section) and so on.

Regards,
Sven

Reply by email