The help is probably not the best written material ... designing code to use
globals is a poor programming practise. What the help is trying to say is
if you protect a resource with a critical section, then you will need to
make sure that any thread that tries to access the resource must first
attempt to lock the critical section. Failure to do so can introduce bugs
that are hard to trace.
Your code will work fine, but you can further improve it by "hiding" the
critical section. I'll give you an example:
Your technique:
TSomeClass = class
public
property SomeResource;
property Requestlock: TCriticalSection;
end;
var
SomeObject: TSomeClass;
To access the resource:
SomeObject.RequestLock.Acquire;
try
Do something with SomeResource;
finally
SomeObject.RequestLock.Release;
end;
Hiding the critical section:
TSomeClass = class
private
FRequestlock: TCriticalSection;
FSomeResource;
public
procedure DoSomethingWithResource;
end;
procedure TSomeObject.RequestLock.DoSomethingWithResource;
begin
FRequestLock.Enter;
try
Do something with FSomeResource;
finally
FRequestLock.Leave;
end;
end;
var
SomeObject: TSomeClass;
To access the resource:
SomeObject.DoSomethingWithResource;
This way, all threads are forced to use the critical section. For another
example, take a look at the VCL code for TThreadList.
Hope this helps.
Dennis.
----- Original Message -----
From: "Stacey Verner" <[EMAIL PROTECTED]>
Sent: Thursday, May 13, 2004 7:36 AM
I the Delphi documentation it says that instances of TCriticalSection
and TMultiReadExclusiveWriteSynchronizer must be global.
Does this mean that they really have to be global (if so why?) or do
they just have to be available to all who are interested?
In my case I have an object which is accessed my multiple threads.
I have the TCriticalSection as a property of the object.
Each thread can go Object1.RequestLock.Acquire and then do whatever it
needs to.
Is this OK?
Stacey
_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi