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

Mailing lists / fpc-pascal / 2017-03
7 messages · 4 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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
This is not foreseen as a language construct, you must do this yourself.
Michael.
I have a class where its instances are shared between multiple threads. How can I declare…
African Wild Dog wroteI 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
Hi,
Michael Schnell wroteAccessing 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
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 :
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 =(.
This is actually entirely platform specific. The underlying implementation of threadvars…
Karoly Balogh wroteThis 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
2017-03-17 5:50 GMT-03:00 Michael Schnell : I have huge collection of objects (abot 1…
African Wild Dog wroteI will have to redesign my classes =(.
Maybe you just can add a field denoting the thread (by n integer) and set this when creating an instance, and later just read it for the appropriate purpose...
-Michael