◆Delphi多线程编程之五不同类线程读写全局变量-阻塞和锁定◆(乌龙哈里2008-10-13)
(调试环境:Delphi2007+WinXPsp3 例程:Tst_Thread5.dpr)
前面的例子都是同类线程的不同实例来读写全局变量,用临界区、互斥等来锁住同段代码。现在碰到的问题是,A,B两个不同类型的线程,如何安全地来读写全局变量。
unit Tst_Thread5U;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,SyncObjs;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TFirstThread=Class(TThread)
protected
procedure Execute;override;
procedure ShowNum;
end;
TSecondThread=Class(TThread)
protected
procedure Execute;override;