[fpc-pascal] Getting the state of a TRTLCriticalSection

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Sun Oct 6 11:26:01 CEST 2013

Is there a preferred way of reading back whether something (including 
the current thread) has already entered a TCriticalSection?

I'm trying to put assertions in code that, partly under non-GUI thread 
control via Synchronize, adds and deletes pages to a TPageControl. If I 
do this

property BloodyGreatLock: boolean read GetBGL write SetBGL;
         fBGL: TCriticalSection;
function GetBGL: boolean;

var     gotLock: boolean;

     gotLock := fBGL.TryEnter
     if gotLock then
   result := not gotLock
end { GetBGL } ;

then this initialisation code and test sequence

   fBGL := TCriticalSection.Create;
   Assert(BloodyGreatLock = false); (* Check lock read works properly  *)
   Assert(BloodyGreatLock = false);
   BloodyGreatLock := true;
   Assert(BloodyGreatLock = true); <===== FAILURE HERE
   Assert(BloodyGreatLock = true);

fails where indicated since the lock is owned by the current thread so 
TryEnter has succeeded.

Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]

More information about the fpc-pascal mailing list