Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Mar : Where to acquire CriticalSection
| Subject: | Where to acquire CriticalSection |
| Posted by: | "edbored" (edwardbno..@nonosgci.com) |
| Date: | Mon, 26 Mar 2007 17:56:26 |
I've started working with multi-threaded code, and I have a basic question
about where to acquire CriticalSection...
Can I wrap the Acquire/Release within the property itself, and just call
it - or do I have to wrap the Acquire/Release around each place where the
property is called.
For example, let's say I have some property
property aSharedVar : integer read GetASharedVar
write SetASharedVar;
Is it safe to implement like this:
function Tdm.GetASharedVar: integer;
begin
CS.Acquire;
try
result:=FaSharedVar;
finally
CS.Release;
end;
end;
procedure Tdm.SetASharedVar(const Value: integer);
begin
CS.Acquire;
try
FaSharedVar := Value;
finally
CS.Release;
end;
end;
Then use it with:
(within ThreadA execute)
ThrA.LocalInt := aSharedVar;
(within ThreadB execute)
ThrB.LocalInt := aSharedVar;
Or do I have to wrap the CS around the callers like this:
Implement property like this:
function Tdm.GetASharedVar: integer;
begin
result:=FaSharedVar;
end;
procedure Tdm.SetASharedVar(const Value: integer);
begin
FaSharedVar := Value;
end;
Then use it with:
(within ThreadA execute)
begin
CS.Acquire;
try
ThrA.LocalInt := aSharedVar;
finally
CS.Release;
end;
(within ThreadB execute)
begin
CS.Acquire;
try
ThrB.LocalInt := aSharedVar;
finally
CS.Release;
end;
TIA.
Cheers,
EdB