У меня есть объект x, к которому нужно получить доступ из нескольких (более 5 потоков). Структура объекта
Tx = class
private
Fx: integer;
public
property x: integer read Fx read Fx;
etc;
Какой способ защиты лучше (самый элегантный):
a)
Tx = class
private
Fx: integer;
public
property x: integer read Fx read Fx;
public
constructor Create; <--- Create the criticalsection here
destructor Destroy; <--destroy it here
etc;
var
cs: TCriticalSection;
Obj: Tx;
function GetSafeObject(): Tx;
begin
CS.Enter;
try
Result:= Obj;
finally
CS.Leave;
end;
и всегда обращаться к объекту как GetSafeObj (). X: = 3;
или
Tx = class
private
Fx: integer;
FCS: TCriticalSection;
public
property x: integer read GerX read SetX;
public
constructor Create; <--- Create the criticalsection here
destructor Destroy; <--destroy it here
etc;
where
function Tx.Getx(): integer;
begin
CS.Enter;
try
Result:= Fx;
finally
CS.Leave;
end;
end;
end;
и всегда обращаться к объекту как обычно. Я думаю, что первый вариант более элегантен, даже если оба метода должны работать нормально. Какие комментарии?