关于TThreadList的一个疑惑 ( 积分: 50 )

  • 主题发起人 主题发起人 masm
  • 开始时间 开始时间
M

masm

Unregistered / Unconfirmed
GUEST, unregistred user!
请看下面简单代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
L:TThreadList;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clGreen;
Finally
L.UnlockList;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clRed;
Finally
L.UnlockList;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
L:=TThreadList.Create;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
L.LockList;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
L.UnlockList;
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
L.Free;
end;
end.
=========================================
我的预期:Button3把L加锁,进入临界区,Button1加锁时应等待L解锁再加锁进入。即按Button3后,再按Button1,Label1的颜色不作任何改变,按Button4.解锁后,Label1颜色改变。
结果不是这样,跟加不加锁好像没关系,L居然纯粹多余。郁闷!!
 
请看下面简单代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
L:TThreadList;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clGreen;
Finally
L.UnlockList;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
L.LockList;
try
Label1.Color:=clRed;
Finally
L.UnlockList;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
L:=TThreadList.Create;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
L.LockList;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
L.UnlockList;
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
L.Free;
end;
end.
=========================================
我的预期:Button3把L加锁,进入临界区,Button1加锁时应等待L解锁再加锁进入。即按Button3后,再按Button1,Label1的颜色不作任何改变,按Button4.解锁后,Label1颜色改变。
结果不是这样,跟加不加锁好像没关系,L居然纯粹多余。郁闷!!
 
楼主理解错了TThreadList.LockList的含义;
locklist是在保证安全的情况下返回threadlist的线程表,
它解决的是在操作线程表的时候安全地处理线程表中是否有线程的加入或者删除的情况;
关于多线程的编程, 先多看一些例子,正确理解基本的概念, 再多做一些这样的测试,其实也不是很难的;
 
后退
顶部