Sign in Join

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

Mailing lists / fpc-pascal / 2017-03

Threadvar member field

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

African Wild Dog

I have a class where its instances are shared between multiple threads. How can I declare one variable per instance per thread?

The code below does not compile (fpc 3.0.0):

TMyClass = class
public
  threadvar MyValue: Integer;
end;

Regards

Reply by email

Michael Van Canneyt
to African Wild Dog
African Wild Dog wrote 9 lines
I have a class where its instances are shared between multiple threads. How can I declare one variable per instance per thread? The code below does not compile (fpc 3.0.0): TMyClass = class public threadvar MyValue: Integer; end;

This is not foreseen as a language construct, you must do this yourself.

Michael.

Reply by email

Michael Schnell
to African Wild Dog: I have a class where its instances are shared between multiple threads. How can I declare…
African Wild Dog wrote
I have a class where its instances are shared between multiple threads. How can I declare one variable per instance per thread?

Does this really make sense ?

Accessing threadvars (in fpc) costs a lot more CPU cycles (i.e. involves an OS call) than accessing normal variables.

Supposedly the way to go is to do per-thread instances of a class, that might reference the other classes you want to instantiate.

-Michael

Reply by email

Karoly Balogh (Charlie/SGR
to Michael Schnell

Hi,

Michael Schnell wrote
Accessing threadvars (in fpc) costs a lot more CPU cycles (i.e. involves an OS call) than accessing normal variables.

This is actually entirely platform specific. The underlying implementation of threadvars is very different for each platform, and highly depends both on the CPU arch and the OS. On some platforms, threadvars are barely slower than normal ones, other platforms have the large penalty you're talking about. I agree though that it's still wise to count on the worst case scenario though.

Charlie

Reply by email

African Wild Dog
to Michael Schnell: Does this really make sense ? Accessing threadvars (in fpc) costs a lot more CPU cycles…

2017-03-17 5:50 GMT-03:00 Michael Schnell :

Show quoted text 14 lines
On 16.03.2017 19:38, African Wild Dog wrote: > I have a class where its instances are shared between multiple threads. > How can I declare one variable per instance per thread? > Does this really make sense ? Accessing threadvars (in fpc) costs a lot more CPU cycles (i.e. involves an OS call) than accessing normal variables. Supposedly the way to go is to do per-thread instances of a class, that might reference the other classes you want to instantiate.

I have huge collection of objects (abot 1 million) in memory so if i go with per-thread instance it will consume the entire memory ram. I will have to redesign my classes =(.

Reply by email

Michael Schnell
to Karoly Balogh (Charlie/SGR: This is actually entirely platform specific. The underlying implementation of threadvars…
Karoly Balogh wrote
This is actually entirely platform specific. The underlying implementation of threadvars is very different for each platform, and highly depends both on the CPU arch and the OS.

That might be true.

Last time I checked (several years ago), the fpc implementations did OS calls when accessing thread vars with X86 architecture for Windows. This is not necessary. All C compilers I checked don't do this, but use a dedicated segment register, even though this (at that time) was not documented by Microsoft. Linux C compilers use a dedicated Segment register, as well, here its a regular documented feature of the OS. (I don't remember what fpc did for Linux at that time.)

Hopefully 64 bit platforms are better (and decently documented) on that behalf.

No idea about ARM etc.

-Michael

Reply by email