W
wind_2005
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:
我今天做两个简单的多线程程序,这两个程序,除了同步方式不同,萁他所有代码都相同,但处理后的结果却不同。这两个程序看起来代码虽长,其实功能很简单,还望各位高人有空能帮我看看,谢谢了!
代码如下:
===================
第一个程序:
===================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Thread1: TSyncThread;
Thread2: TSyncThread;
procedure Thread1Done(Sender: TObject);
procedure Thread2Done(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
const
MaxSize = 100;
var
Form1: TForm1;
CS: TRTLCriticalSection
//临界区变量
GData: array[1..MaxSize] of integer;
iNum: integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
InitializeCriticalSection(CS)
//初始化临界区
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteCriticalSection(CS)
//清除临界区
end;
procedure TForm1.Thread1Done(Sender: TObject);
var
i: integer;
begin
ListBox1.Clear;
for i := 1 to MaxSize do
begin
ListBox1.Items.Add(IntToStr(gdata));
end;
LeaveCriticalSection(CS)
//离开临界区
end;
procedure TForm1.Thread2Done(Sender: TObject);
var
i: integer;
begin
ListBox2.Clear;
for i := 1 to MaxSize do
begin
ListBox2.Items.Add(IntToStr(gdata));
end;
LeaveCriticalSection(CS)
//离开临界区
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Thread1 := TSyncThread.Create(False);
Thread1.OnTerminate := Thread1Done;
Thread2 := TSyncThread.Create(False);
Thread2.OnTerminate := Thread2Done;
end;
end.
--------------------
unit Unit2;
interface
uses
Classes, sysUtils, windows;
type
TSyncThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute
override;
end;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TSyncThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end
}
{ TSyncThread }
procedure TSyncThread.Execute;
var
i: integer;
begin
FreeOnTerminate := True;
EnterCriticalSection(CS)
//进入临界区
for i := 1 to MaxSize do
begin
Inc(iNum);
GData := iNum;
sleep(5);
end;
end;
end.
===================
第二个程序:
===================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Thread1: TSyncThread;
Thread2: TSyncThread;
procedure Thread1Done(Sender: TObject);
procedure Thread2Done(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
const
MaxSize = 100;
var
Form1: TForm1;
HMutex: THandle
//互斥对象句柄
GData: array[1..MaxSize] of integer;
iNum: integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
HMutex := CreateMutex(nil,False,nil)
//创建互斥对象
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(HMutex)
//释放互斥对象
end;
procedure TForm1.Thread1Done(Sender: TObject);
var
i: integer;
begin
ListBox1.Clear;
for i := 1 to MaxSize do
begin
ListBox1.Items.Add(IntToStr(gdata));
end;
ReleaseMutex(HMutex)
//解除互斥
end;
procedure TForm1.Thread2Done(Sender: TObject);
var
i: integer;
begin
ListBox2.Clear;
for i := 1 to MaxSize do
begin
ListBox2.Items.Add(IntToStr(gdata));
end;
ReleaseMutex(HMutex)
//解除互斥
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Thread1 := TSyncThread.Create(False);
Thread1.OnTerminate := Thread1Done;
Thread2 := TSyncThread.Create(False);
Thread2.OnTerminate := Thread2Done;
end;
end.
-------------------
unit Unit2;
interface
uses
Classes, sysUtils, windows;
type
TSyncThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute
override;
end;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TSyncThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end
}
{ TSyncThread }
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 MaxSize do
begin
Inc(iNum);
GData := iNum;
sleep(5);
end;
end;
end.
------------------
不知是我的代码写错了,还是本该是这样的结果?为什么?
谢谢!
我今天做两个简单的多线程程序,这两个程序,除了同步方式不同,萁他所有代码都相同,但处理后的结果却不同。这两个程序看起来代码虽长,其实功能很简单,还望各位高人有空能帮我看看,谢谢了!
代码如下:
===================
第一个程序:
===================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Thread1: TSyncThread;
Thread2: TSyncThread;
procedure Thread1Done(Sender: TObject);
procedure Thread2Done(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
const
MaxSize = 100;
var
Form1: TForm1;
CS: TRTLCriticalSection
//临界区变量
GData: array[1..MaxSize] of integer;
iNum: integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
InitializeCriticalSection(CS)
//初始化临界区
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteCriticalSection(CS)
//清除临界区
end;
procedure TForm1.Thread1Done(Sender: TObject);
var
i: integer;
begin
ListBox1.Clear;
for i := 1 to MaxSize do
begin
ListBox1.Items.Add(IntToStr(gdata));
end;
LeaveCriticalSection(CS)
//离开临界区
end;
procedure TForm1.Thread2Done(Sender: TObject);
var
i: integer;
begin
ListBox2.Clear;
for i := 1 to MaxSize do
begin
ListBox2.Items.Add(IntToStr(gdata));
end;
LeaveCriticalSection(CS)
//离开临界区
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Thread1 := TSyncThread.Create(False);
Thread1.OnTerminate := Thread1Done;
Thread2 := TSyncThread.Create(False);
Thread2.OnTerminate := Thread2Done;
end;
end.
--------------------
unit Unit2;
interface
uses
Classes, sysUtils, windows;
type
TSyncThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute
override;
end;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TSyncThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end
}
{ TSyncThread }
procedure TSyncThread.Execute;
var
i: integer;
begin
FreeOnTerminate := True;
EnterCriticalSection(CS)
//进入临界区
for i := 1 to MaxSize do
begin
Inc(iNum);
GData := iNum;
sleep(5);
end;
end;
end.
===================
第二个程序:
===================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Thread1: TSyncThread;
Thread2: TSyncThread;
procedure Thread1Done(Sender: TObject);
procedure Thread2Done(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
const
MaxSize = 100;
var
Form1: TForm1;
HMutex: THandle
//互斥对象句柄
GData: array[1..MaxSize] of integer;
iNum: integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
HMutex := CreateMutex(nil,False,nil)
//创建互斥对象
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CloseHandle(HMutex)
//释放互斥对象
end;
procedure TForm1.Thread1Done(Sender: TObject);
var
i: integer;
begin
ListBox1.Clear;
for i := 1 to MaxSize do
begin
ListBox1.Items.Add(IntToStr(gdata));
end;
ReleaseMutex(HMutex)
//解除互斥
end;
procedure TForm1.Thread2Done(Sender: TObject);
var
i: integer;
begin
ListBox2.Clear;
for i := 1 to MaxSize do
begin
ListBox2.Items.Add(IntToStr(gdata));
end;
ReleaseMutex(HMutex)
//解除互斥
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Thread1 := TSyncThread.Create(False);
Thread1.OnTerminate := Thread1Done;
Thread2 := TSyncThread.Create(False);
Thread2.OnTerminate := Thread2Done;
end;
end.
-------------------
unit Unit2;
interface
uses
Classes, sysUtils, windows;
type
TSyncThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute
override;
end;
implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TSyncThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end
}
{ TSyncThread }
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 MaxSize do
begin
Inc(iNum);
GData := iNum;
sleep(5);
end;
end;
end.
------------------
不知是我的代码写错了,还是本该是这样的结果?为什么?
谢谢!