Threadvar member field

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Threadvar member field

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Threadvar member field

Michael Van Canneyt


On Thu, 16 Mar 2017, 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?
>
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Threadvar member field

Michael Schnell
In reply to this post by African Wild Dog
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.

-Michael

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Threadvar member field

Karoly Balogh (Charlie/SGR)
Hi,

On Fri, 17 Mar 2017, 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Threadvar member field

African Wild Dog
In reply to this post by Michael Schnell
2017-03-17 5:50 GMT-03:00 Michael Schnell <[hidden email]>:
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 =(.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Threadvar member field

Michael Schnell
In reply to this post by Karoly Balogh (Charlie/SGR)
On 17.03.2017 17:13, Karoly Balogh (Charlie/SGR) 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Threadvar member field

Michael Schnell
In reply to this post by African Wild Dog

On 17.03.2017 23:51, African Wild Dog wrote:

I 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...