Код ниже - это RAII wrapper для критических секций в Delphi, - был сгенерирован chatgpt. Диалог с ChatGPT доступен по ссылке: https://chatgpt.com/share/6981f74e-422c-8013-9cd6-9a8f4518e18a.
type ICriticalSectionGuard = interface end;
TCriticalSectionGuard = class(TInterfacedObject, ICriticalSectionGuard) private FCritSec: TCriticalSection; public constructor Create(ACritSec: TCriticalSection); destructor Destroy; override; end;
constructor TCriticalSectionGuard.Create(ACritSec: TCriticalSection); begin inherited Create; FCritSec := ACritSec; FCritSec.Enter; end;
destructor TCriticalSectionGuard.Destroy; begin FCritSec.Leave; inherited; end;
procedure Guard(const ACritSec: TCriticalSection; out AGuard: ICriticalSectionGuard); begin AGuard := TCriticalSectionGuard.Create(ACritSec); end;
|
Использование:
var g: ICriticalSectionGuard; begin Guard(FCritSec, g);
Work; end;
|