我使用WaitForMultipleObjects函数等待几个线程的完成,但我做了一些错误的事情,因为结果不是预期的
请参阅此示例代码
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
end;
TFoo = class(TThread)
private
Factor: Double;
procedure ShowData;
protected
procedure Execute; override;
constructor Create(AFactor : Double);
end;
var
Form1: TForm1;
implementation
Uses
Math;
{$R *.dfm}
{ TFoo }
constructor TFoo.Create(AFactor: Double);
begin
inherited Create(False);
Factor := AFactor;
FreeOnTerminate := True;
end;
procedure TFoo.Execute;
const
Max=100000000;
var
i : Integer;
begin
inherited;
for i:=1 to Max do
Factor:=Sqrt(Factor);
Synchronize(ShowData);
end;
procedure TFoo.ShowData;
begin
Form1.Memo1.Lines.Add(FloatToStr(Factor));
end;
procedure TForm1.Button1Click(Sender: TObject);
const
nThreads=5;
Var
tArr : Array[1..nThreads] of TFoo;
hArr : Array[1..nThreads] of THandle;
i : Integer;
rWait : Cardinal;
begin
for i:=1 to nThreads do
begin
tArr[i]:=TFoo.Create(Pi*i);
hArr[i]:=tArr[i].Handle;
end;
repeat
rWait:= WaitForMultipleObjects(nThreads, @hArr, True, 100);
Application.ProcessMessages;
until rWait<>WAIT_TIMEOUT;
//here I want to show this message when all the threads are terminated
Memo1.Lines.Add('Wait done');
end;
end.这是演示应用程序的当前输出
1
Wait done
1
1
1
1但是我想要这样的东西
1
1
1
1
1
Wait done如何使用WaitForMultipleObjects函数等待所有线程终止?
发布于 2011-07-29 08:14:35
修复:删除FreeOnTerminate。
当您仍然需要句柄时,您的代码会导致线程被释放。这是一个很大的bug,您可以在代码中的其他位置获得访问冲突,或者从WaitFormMultipleObjects返回错误返回代码。
当TThread被释放时,TThread.handle变得无效,这会提前终止等待循环,因为句柄不再有效。如果你试图在后台释放TThread之后访问它,你也可能会遇到访问冲突,所以我认为在已知的时间故意释放它们会更好。
使用线程句柄作为事件句柄可以工作得很好,但是当线程终止时,不应该使用FreeOnTerminate来释放线程,因为这会太快地破坏句柄。
我也同意一些人所说的,用Application.Processmessages做一个忙碌的等待循环是相当丑陋的。还有其他方法可以做到这一点。
unit threadUnit2;
interface
uses Classes, SyncObjs,Windows, SysUtils;
type
TFoo = class(TThread)
private
FFactor: Double;
procedure ShowData;
protected
procedure Execute; override;
constructor Create(AFactor : Double);
destructor Destroy; override;
end;
procedure WaitForThreads;
implementation
Uses
Forms,
Math;
procedure Trace(msg:String);
begin
if Assigned(Form1) then
Form1.Memo1.Lines.Add(msg);
end;
{ TFoo }
constructor TFoo.Create(AFactor: Double);
begin
inherited Create(False);
FFactor := AFactor;
// FreeOnTerminate := True;
end;
destructor TFoo.Destroy;
begin
inherited;
end;
procedure TFoo.Execute;
const
Max=100000000;
var
i : Integer;
begin
inherited;
for i:=1 to Max do
FFactor:=Sqrt(FFactor);
Synchronize(ShowData);
end;
procedure TFoo.ShowData;
begin
Trace(FloatToStr(FFactor));
end;
procedure WaitForThreads;
const
nThreads=5;
Var
tArr : Array[1..nThreads] of TFoo;
hArr : Array[1..nThreads] of THandle;
i : Integer;
rWait : Cardinal;
begin
for i:=1 to nThreads do
begin
tArr[i]:=TFoo.Create(Pi*i);
hArr[i]:=tArr[i].handle; // Event.Handle;
end;
repeat
rWait:= WaitForMultipleObjects(nThreads, @hArr[1],{waitAll} True, 150);
Application.ProcessMessages;
until rWait<>WAIT_TIMEOUT;
Sleep(0);
//here I want to show this message when all the threads are terminated
Trace('Wait done');
for i:=1 to nThreads do
begin
tArr[i].Free;
end;
end;
end.发布于 2011-07-29 14:48:02
如果你真的想了解多线程是如何工作的,那么你走上了一条正确的道路--通过代码学习并提出问题,就像你在这里所做的那样。然而,如果你只是想在你的应用程序中使用多线程,你可以用OmniThreadLibrary以更简单的方式来实现,前提是你至少使用了Delphi2009。
uses
Math,
OtlTask,
OtlParallel;
function Calculate(factor: real): real;
const
Max = 100000000;
var
i: integer;
begin
Result := factor;
for i := 1 to Max do
Result := Sqrt(Result);
end;
procedure TForm35.btnClick(Sender: TObject);
const
nThreads = 5;
begin
Parallel.ForEach(1, nThreads).Execute(
procedure (const task: IOmniTask; const value: integer)
var
res: real;
begin
res := Calculate(Pi*value);
task.Invoke(
procedure begin
Form35.Memo1.Lines.Add(FloatToStr(res));
end
);
end
);
Memo1.Lines.Add('All done');
end;发布于 2011-07-29 15:39:38
这就是正在发生的事情。
WaitForMultipleObjects.WAIT_FAILED返回错误代码6,句柄无效。WaitForMultipleObjects的唯一句柄是线程句柄,因此其中一个线程句柄无效。FreeOnTerminate.来关闭句柄
这个故事的寓意是检查所有函数的返回值是否正确,并让GetLastError引导您找到问题的根本原因。
https://stackoverflow.com/questions/6867105
复制相似问题