,第一次提问,一个两个线程创建互斥关系的例子,但是没达到预期效果,请高手指点(100分)

  • 主题发起人 主题发起人 编程菜鸟2008
  • 开始时间 开始时间

编程菜鸟2008

Unregistered / Unconfirmed
GUEST, unregistred user!
代码如下
用两个Listbox分别来显示1-100和2-200,用两个线程控制,在他们之间创建了互斥关系,但是运行后两个显示的反而相同,不知道为什么,请高手指点
unit thread3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls;
type
TSyncThread=class(TThread)
protected
procedure execute;override;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Thread1,Thread2:TSyncThread;
procedure Thread1done(sender:TObject);
procedure Thread2done(sender:Tobject);
{ Private declarations }
public
{ Public declarations }
end;
const maxsize=100;
var
Form1: TForm1;
Globaldata:array[1..maxsize] of integer;
Incnum:integer=0;
Hmutex:Thandle=0;
implementation
{$R *.dfm}
procedure TsyncThread.execute;
var
i:integer;
waitreturn:DWORD;
begin
FreeOnTerminate:=true;
waitreturn:=WaitForsingleObject(Hmutex,INFINITE);
if waitreturn=Wait_Object_0 then
for i:=1 to maxsizedo
begin
Inc(Incnum);
Globaldata:=Incnum;
sleep(10);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Hmutex:=CreateMutex(nil,false,nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
closehandle(Hmutex);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Thread1:=TsyncThread.Create(false);
Thread1.OnTerminate:=Thread1done;
Thread2:=TsyncThread.Create(false);
Thread2.OnTerminate:=Thread2done;
end;
procedure Tform1.Thread1done(sender:Tobject);
var
i:integer;
begin
listbox1.Items.Clear;
for i:=1 to maxsizedo
listbox1.Items.Add(inttostr(Globaldata));
ReleaseMutex(Hmutex);
end;
procedure Tform1.Thread2done(sender:Tobject);
var
i:integer;
begin
listbox2.Items.Clear;
for i:=1 to maxsizedo
listbox2.Items.Add(inttostr(Globaldata));
ReleaseMutex(Hmutex);
end;
end.
请帮忙,初次发贴
 
基本概念根本不清楚
procedure TsyncThread.execute;
var
aInt: integer;
WaitRetVal: DWORD;
begin
//FreeOnTerminate:=true;
//这个标志一般应该由线程外部控制
aInt:=0;
while not Terminateddo
begin
WaitRetVal:=WaitForsingleObject(Hmutex, ? );//无限等待不需要处理返回值,不好
if WaitRetVal = Wait_Object_0 then
begin
Inc(Incnum);
Globaldata:=Incnum;

ReleaseMetux(hMetux);
//记得要释放
sleep(10);
end;
end;
end;
 
//FreeOnTerminate:=true;
//这个标志一般应该由线程外部控制
放到init里也可以。是不是?
 
谢谢楼上的楼上和楼上
是的,我对线程和进程的概念还不是不大理解,那些函数更不用说了
能不能再详细和我说说??谢谢了
学delphi一个多月,都是一个人看书摸索,头痛
 
为什么显示的是相同的??
我不是已经创建了互斥关系了吗
可是显示的数字却是相同的啊,这是为什么???
 
我调试了一下,把 ReleaseMutex(Hmutex);
解除互斥放在
procedure TsyncThread.execute;
var
i:integer;
waitreturn:DWORD;
begin
FreeOnTerminate:=true;
waitreturn:=WaitForsingleObject(Hmutex,INFINITE);
if waitreturn=Wait_Object_0 then
for i:=1 to maxsizedo
begin
Inc(Incnum);
Globaldata:=Incnum;
sleep(10);
end;
ReleaseMutex(Hmutex);
end;
就可以了,应该是第二个线程执行时还没有释放租塞吧
 

WaitRetVal:=WaitForsingleObject(Hmutex, ? );//无限等待不需要处理返回值,不好
if WaitRetVal = Wait_Object_0 then
//修改数据了
else

//你没有处理呀...?
....
所以当第二个线程去取的时候没得到,但是你没有处理这个异常..
我帮你改成下面这样就行了..
procedure TsyncThread.execute;
var
i:integer;
waitreturn:DWORD;
begin
FreeOnTerminate:=true;
while Waitreturn <> Wait_Object_0do

begin
waitreturn:=WaitForsingleObject(Hmutex,INFINITE);
if waitreturn=Wait_Object_0 then
for i:=1 to maxsizedo
begin
Inc(Incnum);
Globaldata:=Incnum;
sleep(1);
end;
end;
//ykxiong1在这里 releassemutx 也不对吧.呵呵
end;

其它的都不用动了.
 
谢谢大家
好久没来看这帖子了
现在终于明白了
 
多人接受答案了。
 
后退
顶部