Do class instances own TMultiReadExclusiveWriteSynchronizer instances?

I have a multi-threaded Delphi program that creates several instances of some classes, and I want each class instance to have its own instance of TMultiReadExclusiveWriteSynchronizer for use in get and set methods with specific properties.

eg. Here is the part of the unit in which I use TMultiReadExclusiveWriteSynchronizer in one class:

interface

TSGThread=class(TThread)
private
    fWaiting:boolean;
    function getWaiting:boolean;
    procedure setWaiting(value:boolean);
public
    property waiting:boolean read getWaiting write setWaiting;
end;

implementation

var syncWaiting:TMultiReadExclusiveWriteSynchronizer;

function TSGThread.getWaiting:boolean;
begin
    syncWaiting.BeginRead;
    result:=fWaiting;
    syncWaiting.EndRead;
end;

procedure TSGThread.setWaiting(value:boolean);
begin
    syncWaiting.BeginWrite;
    fWaiting:=value;
    syncWaiting.EndWrite;
end;

initialization
    syncWaiting:=TMultiReadExclusiveWriteSynchronizer.Create;
finalization
    syncWaiting.Free;
end.

, TMultiReadExclusiveWriteSynchronizer, TSGThread.
TSGThread.
Thread A Thread B, , , , , , .

Delphi Help : " TMultiReadExclusiveWriteSynchronizer", , ?
, TMultiReadExclusiveWriteSynchronizer ?

+3
3

, . , . - , .

+2

:

TMultiReadExclusiveWriteSynchronizer, , .

. - , . , . , .

+4

, , , . / .

, , ,

sgThread.Waiting := not sgThread.Waiting;

.

Now back to the topic. The TMultiReadExclusiveWriteSynchronizer scope must be the same as the resource that it is protecting. Since you want to protect the private field of an object, you can declare TMultiReadExclusiveWriteSynchronizer as a private field. (You protect access to the private field, not access to this property)

+3
source

All Articles