我正在用Delphi编写一个多线程应用程序,需要使用一些东西来保护共享资源。
在锁中,我会使用“C#”关键字:
private someMethod() {
lock(mySharedObj) {
//...do something with mySharedObj
}
}在Delphi中我找不到任何类似的东西,我只找到了TThread.Synchronize( someMethod )方法,它通过在主VCL线程中调用someMethod来防止潜在的冲突,但这并不是我想要做的……
编辑:我使用的是Delphi 6
发布于 2010-06-11 21:26:04
幸运的是,在Delphi 6中不能锁定任意对象(尽管在更新的版本,2009和更高版本中可以),因此需要有一个单独的锁对象,通常是临界区。
TCriticalSection (注意:文档来自FreePascal,但它也存在于Delphi中):
示例代码:
type
TSomeClass = class
private
FLock : TCriticalSection;
public
constructor Create();
destructor Destroy; override;
procedure SomeMethod;
end;
constructor TSomeClass.Create;
begin
FLock := TCriticalSection.Create;
end;
destructor TSomeClass.Destroy;
begin
FreeAndNil(FLock);
end;
procedure TSomeClass.SomeMethod;
begin
FLock.Acquire;
try
//...do something with mySharedObj
finally
FLock.Release;
end;
end;发布于 2010-06-11 22:34:28
在Delphi6中没有类似的东西。从Delphi2009开始,你可以使用System.TMonitor方法来获取任意对象的锁。
System.TMonitor.Enter(obj);
try
// ...
finally
System.TMonitor.Exit(obj);
end;(您需要“系统”前缀,因为TMonitor名称与Forms单元中的类型冲突。另一种方法是使用全局MonitorEnter和MonitorExit函数。)
发布于 2010-06-11 21:45:24
虽然不像c#那么简单,但下面的代码可能对你有用。
with Lock(mySharedObj) do
begin
//...do something with mySharedObj
UnLock;
end;简而言之,
Lock(mySharedObj)时,将在内部列表中搜索现有锁。如果没有找到现有的锁,则会创建一个新的锁。如果另一个线程仍然拥有该锁,则新线程将被阻塞。Unlock是必需的,因为我们不能确定对ILock实例的引用只会在调用的方法的末尾处超出作用域Lock。(如果可以,可以删除Unlock )。请注意,在此设计中,将为您希望保护的每个对象实例创建一个TLock,而不会在应用程序终止之前将其释放。
这可能会被考虑在内,但这将涉及到_AddRef & _Release的混乱。
unit uLock;
interface
type
ILock = interface
['{55C05EA7-D22E-49CF-A337-9F989006D630}']
procedure UnLock;
end;
function Lock(const ASharedObj: TObject): ILock;
implementation
uses
syncobjs, classes;
type
_ILock = interface
['{BAC7CDD2-0660-4375-B673-ECFA2BA0B888}']
function SharedObj: TObject;
procedure Lock;
end;
TLock = class(TInterfacedObject, ILock, _ILock)
private
FCriticalSection: TCriticalSection;
FSharedObj: TObject;
function SharedObj: TObject;
public
constructor Create(const ASharedObj: TObject);
destructor Destroy; override;
procedure Lock;
procedure UnLock;
end;
var
Locks: IInterfaceList;
InternalLock: TCriticalSection;
function Lock(const ASharedObj: TObject): ILock;
var
I: Integer;
begin
InternalLock.Acquire;
try
//***** Does a lock exists for given Shared object
for I := 0 to Pred(Locks.Count) do
if (Locks[I] as _ILock).SharedObj = ASharedObj then
begin
Result := ILock(Locks[I]);
Break;
end;
//***** Create and add a new lock for the shared object
if not Assigned(Result) then
begin
Result := TLock.Create(ASharedObj);
Locks.Add(Result);
end;
finally
InternalLock.Release;
end;
(Result as _ILock).Lock;
end;
{ TLock }
constructor TLock.Create(const ASharedObj: TObject);
begin
inherited Create;
FSharedObj := ASharedObj;
FCriticalSection := TCriticalSection.Create;
end;
destructor TLock.Destroy;
begin
FCriticalSection.Free;
inherited Destroy;
end;
procedure TLock.Lock;
begin
FCriticalSection.Acquire;
end;
function TLock.SharedObj: TObject;
begin
Result := FSharedObj;
end;
procedure TLock.UnLock;
begin
FCriticalSection.Release;
end;
initialization
Locks := TInterfaceList.Create;
InternalLock := TCriticalSection.Create;
finalization
InternalLock.Free;
Locks := nil
end.https://stackoverflow.com/questions/3022978
复制相似问题