在此代码中:
TMyClass = class(TThread)
public
FInputBuffer : TThreadedQueue<TBytes>;
protected
procedure Execute; override;
end;
使用(在TMyClass和其他类中)FInputBuffer是否是线程安全的?
编辑:
样本使用:在TMyClass中:
procedure TMyClass.Execute;
var x :TBytes;
begin
inherited;
FInputBuffer:= TThreadedQueue<TBytes>.Create;
while not Terminated do begin
if FInputBuffer.QueueSize > 0 then begin
x:= FInputBuffer.PopItem;
//some code to use x
end;
end;
FInputBuffer.Free;
end;
在其他课程中:
var MyClass :TMyClass ;
procedure TForm1.btn1Click(Sender: TObject);
var x :TBytes;
begin
//set x
MyClass.FInputBuffer.PushItem(x);
end;
答案 0 :(得分:11)
如果在线程开始运行之前在线程构造函数中创建了FInputBuffer
,并且在线程运行完成后在线程析构函数中被释放,那么是的,当TMyClass
对象仍然存活时,从其他线程访问它是线程安全的,因为TThreadedQueue
为其内部内容提供了自己的线程安全性。您所展示的是完全有效地使用多线程队列,前提是MyClass
变量在调用btn1Click()
时有效。
但是,如果FInputBuffer
在Execute()
内创建,则 是线程安全的,因为btn1Click()
可能尝试在线程开始运行之前,在创建FInputBuffer
之前访问队列。这就是你需要在构造函数中创建FInputBuffer
的原因,例如:
TMyClass = class(TThread)
public
FInputBuffer: TThreadedQueue<TBytes>;
constructor Create(ACreateSuspended: Boolean); override;
destructor Destroy; override;
protected
procedure Execute; override;
end;
constructor TMyClass.Create(ACreateSuspended: Boolean);
begin
inherited;
FInputBuffer := TThreadedQueue<TBytes>.Create;
end;
destructor TMyClass.Destroy;
begin
FInputBuffer.Free;
inherited;
end;
procedure TMyClass.Execute;
var
x: TBytes;
begin
while not Terminated do begin
if FInputBuffer.QueueSize > 0 then begin
x := FInputBuffer.PopItem;
// some code to use x
end;
end;
end;
如果要在FInputBuffer
内创建Execute()
,那么线程应该暴露一个在实际创建FInputBuffer
后设置的标志/信号,然后没有其他代码应该尝试访问FInputBuffer
,直到设置了该标志/信号。创建线程实例的代码应该在将控制权返回给代码的其余部分之前等待该标志/信号,例如:
TMyClass = class(TThread)
public
FInputBuffer: TThreadedQueue<TBytes>;
FInputBufferCreated: TEvent;
constructor Create(ACreateSuspended: Boolean); override;
destructor Destroy; override;
protected
procedure Execute; override;
procedure DoTerminate; override;
end;
constructor TMyClass.Create(ACreateSuspended: Boolean);
begin
inherited;
FInputBufferCreated := TEvent.Create(nil, True, False, '');
end;
destructor TMyClass.Destroy;
begin
FInputBufferCreated.Free;
inherited;
end;
procedure TMyClass.Execute;
var
x: TBytes;
begin
FInputBuffer := TThreadedQueue<TBytes>.Create;
FInputBufferCreated.SetEvent;
while not Terminated do begin
if FInputBuffer.QueueSize > 0 then begin
x := FInputBuffer.PopItem;
// some code to use x
end;
end;
end;
procedure TMyClass.DoTerminate;
begin
if FInputBufferCreated <> nil then
FInputBufferCreated.ResetEvent;
FreeAndNil(FInputBuffer);
inherited;
end;
var
MyClass: TMyClass = nil;
procedure TForm1.StartBufferThread;
var
I: Integer;
begin
MyClass := TMyClass.Create(False);
if MyClass.FInputBufferCreated.WaitFor(2500) <> wrSignaled then
begin
MyClass.Terminate;
MyClass.WaitFor;
FreeAndNil(MyClass);
raise Exception.Create('MyClass.FInputBuffer not created after 2.5 seconds!');
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
x: TBytes;
begin
//set x
if MyClass <> nil then
MyClass.FInputBuffer.PushItem(x);
end;