【发布时间】:2010-11-09 01:54:33
【问题描述】:
根据《Delphi 2009 中的新增功能》,有一个新的调试器功能称为“等待链遍历”。它特别指出“已添加等待链遍历功能以帮助您解决线程争用或死锁问题。该功能依赖于添加到 Windows Vista 操作系统的工具,该工具向调试器提供有关应用程序线程等待状态的信息等待链的形式。”
Delphi 2009 是在 Windows Vista 是当前操作系统时发布的。根据我的经验,Vista 中引入的大部分功能在 Windows 7 中也可用。但是,我在 Delphi 2009 中通过 Delphi XE 安装(都在 Windows 7 上)没有看到此功能。
我正在调试器的“线程”窗格中寻找此功能。
我是否在正确的位置寻找等待链遍历?
它是真正仅在 Windows Vista 中可用而在 Windows 7 中不可用的功能吗?
David M 提供了一个很好且清晰的答案,但我仍然没有在“线程”窗格中看到“等待链”列。这是一些代码。
主窗体:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SyncObjs, RanThread;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ThreadDone(Sender: TObject);
end;
var
Form1: TForm1;
RanGenThread: TRandomizer;
implementation
uses LoadThread;
{$R *.dfm}
{ TForm1 }
procedure TForm1.ThreadDone(Sender: TObject);
begin
RanGenThread.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Sorted := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Thread: TLoader;
begin
ListBox1.Items.Clear;
ListBox1.Sorted := False;
RanGenThread := TRandomizer.Create(True);
RanGenThread.ArraySize := 1000;
Thread := TLoader.Create(True);
with Thread do
begin
RanGenThread.WaitThread := Thread;
FreeOnTerminate := True;
OnTerminate := ThreadDone;
WaitForThread := RanGenThread;
//Use Start in Delphi 2010 or later, where Resume is deprecated
Resume;
end;
RanGenThread.Resume;
end;
initialization
Randomize;
end.
随机化器:
unit RanThread;
interface
uses
Classes, Math, SyncObjs;
type
TRandomizer = class(TThread)
private
{ Private declarations }
FArraySize: Integer;
protected
procedure Execute; override;
public
WaitThread: TThread;
RandNumbers: array of Integer;
property ArraySize: Integer read FArraySize write FArraySize;
end;
implementation
uses Main;
procedure TRandomizer.Execute;
var
i: Integer;
LowNum, HighNum: Integer;
RandNum: Integer;
begin
if FArraySize = 0 then
begin
Exit;
end;
SetLength(RandNumbers, FArraySize);
LowNum := Low(RandNumbers);
HighNum := High(RandNumbers);
//initialize the array
for i := LowNum to HighNum do
RandNumbers[i] := -1;
// generate the random order
for i := LowNum to HighNum do
while True do
begin
RandNum := RandomRange(LowNum, HighNum + 1);
if RandNumbers[RandNum] = -1 then
begin
RandNumbers[RandNum] := i + 1;
break;
end; // if
end; // while
WaitThread.WaitFor;
end;
end.
TLoader:
unit LoadThread;
interface
uses
Classes, SyncObjs, Dialogs, SysUtils, RanThread;
type
TLoader = class(TThread)
private
FWaitForThread: TRandomizer;
procedure UpdateList;
{ Private declarations }
protected
procedure Execute; override;
public
property WaitForThread: TRandomizer
read FWaitForThread write FWaitForThread;
end;
implementation
uses Main;
procedure TLoader.UpdateList;
var
i: Integer;
begin
for i := Low(FWaitForThread.RandNumbers) to
High(FWaitForThread.RandNumbers) do
Form1.ListBox1.Items.Add(IntToStr(FWaitForThread.RandNumbers[i]));
end;
procedure TLoader.Execute;
begin
if WaitForThread <> nil then
begin
FWaitForThread.WaitFor;
Synchronize(UpDateList)
end;
end;
end.
根据 David M 链接的等待链遍历文档,WTC 可用于以下同步对象:
- ALPC
- COM
- 关键部分
- 互斥体
- 发送消息
- 等待进程和线程上的操作
我的代码在一个线程上等待,但它是一个 TThread,而不是直接的本机线程。今晚我将修改我的代码示例,使其在等待 Mutex 时出现死锁,并查看这是否会导致“线程”窗格中出现“等待链”列。
好的。终于有时间进行下一次测试了。创建了一个在启动时拥有 Mutex 所有权的应用程序。创建了一个工作线程,该线程使用 OpenMutex 来获取该 Mutex 的句柄,然后调用 WaitForsingleObject(handle, INFINITE)。 “线程”窗格中仍然没有等待链列。
【问题讨论】:
-
我添加了标签“delphi”,因为这是一个更通用的标签,您的问题是关于几个版本(2009、2010、XE)。我有一个过滤器在 SO 上显示“delphi”标签,而这个问题并没有显示出来 - 我偶然遇到了它。
标签: multithreading delphi debugging delphi-2009