Hello, I'm trying to recreate the c++ lock_guard with this naive implementation
TLockGuard = class(TInterfacedObject)
private
var _mtx: TMutex;
_name: String;
public
constructor Create(mtx: TMutex; name: String = '');
destructor Destroy; override;
end;
constructor TLockGuard.Create(mtx: TMutex; name: String);
begin
inherited Create;
_mtx := mtx;
_name := name;
if assigned(mtx) then begin
loginfo(format('acquiring mutex %s', [_name]), 'thread');
_mtx.Acquire;
loginfo(format('acquired mutex %s', [_name]), 'thread');
end
else raise Exception.Create('mutex unassigned');
end;
destructor TLockGuard.Destroy;
begin
if assigned(_mtx) then begin
loginfo(format('releasing mutex %s', [_name]), 'thread');
try
_mtx.Release;
except
on E: Exception do loginfo(E.toString, 'thread');
end;
loginfo(format('released mutex %s', [_name]), 'thread');
end;
inherited Destroy;
end;
but when Destroy is called and it tries to release the mutex I get this error:
Is there a way to move ownership of the mutex?