下面是双线程互斥,利用一个boolean变量
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
tmythread=class(tthread)
private
process:integer;
fbutton:tbutton;
public
procedure execute;override;
constructor create(tf:boolean;button:tbutton);
end;
var
Form1: TForm1;
b:boolean=true;
i:integer;
mythread:array[1..2] of tmythread;
a:char;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
a:='a';
for i:=1 to 2do
mythread:=tmythread.create(false,button1);
end;
constructor tmythread.create(tf:boolean;button:tbutton);
begin
inherited create(tf);
fbutton:=button;
end;
procedure tmythread.execute;
begin
b:=not b;
while bdo
;
if a='a' then
a:='b'
else
a:='c';
fbutton.caption:=a;
sleep(100);
b:=not b;
end;
end.