Jump to content
chkaufmann

TObject, InstanceSize and thread synchronization

Recommended Posts

A TObject has an InstanceSize of 8 bytes. I searched the internet to understand why. 4 bytes would make sence (pointer to the classtype or whatever).

 

All I find are old postings about this change back in 2009, but is this still valid for the current version? And if the second pointer is available for object locking, can somebody point me to an explanation for this. To be honest, I did not really understand how this works.

 

Currently I use a "Spring.Lock" in all my objects for thread synchronization, but maybe this is not necessary and I could use the built in lock feature of TObject?

 

Christian

Share this post


Link to post
31 minutes ago, chkaufmann said:

A TObject has an InstanceSize of 8 bytes. I searched the internet to understand why. 4 bytes would make sence (pointer to the classtype or whatever).

The extra bytes are for a hidden pointer to a System.TMonitor instance (not to be confused with Vcl.Forms.TMonitor).  Every TObject instance has an optional TMonitor instance associated with it.  The actual TMonitor instance is created the 1st time it is accessed, but the parent TObject's InstanceSize includes space for a pointer to the TMonitor instance.

Quote

All I find are old postings about this change back in 2009, but is this still valid for the current version?

AFAIK, yes.

Quote

And if the second pointer is available for object locking, can somebody point me to an explanation for this.

Read the TMonitor documentation, and also see What is TMonitor in Delphi System unit good for?

Quote

Currently I use a "Spring.Lock" in all my objects for thread synchronization, but maybe this is not necessary and I could use the built in lock feature of TObject?

You could, yes.  You would simply call TMonitor.Enter(obj) and TMonitor.Exit(obj) where needed.

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post
On 8/29/2022 at 7:34 PM, Remy Lebeau said:

The extra bytes are for a hidden pointer to a System.TMonitor instance (not to be confused with Vcl.Forms.TMonitor).  Every TObject instance has an optional TMonitor instance associated with it.  The actual TMonitor instance is created the 1st time it is accessed, but the parent TObject's InstanceSize includes space for a pointer to the TMonitor instance.

AFAIK, yes.

Read the TMonitor documentation, and also see What is TMonitor in Delphi System unit good for?

You could, yes.  You would simply call TMonitor.Enter(obj) and TMonitor.Exit(obj) where needed.

When I read https://www.delphitools.info/2013/06/06/tmonitor-vs-trtlcriticalsection/, then I should not use TMonitor. Is this still the case? Is there another simpler solution to lock an object in order to avoid that multiple threads change an internal value. Locktime will be very short and conflicts probably happen very rarely.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×