月
月满C楼
Unregistered / Unconfirmed
GUEST, unregistred user!
//线程Execute过程中报I/O错误
//Unit Define 中定义全局变量
unit Define;
interface
uses SysUtils;
var
glo_x,glo_y:integer;
mReWsyn:TMultiReadExclusiveWriteSynchronizer;
F:TextFile;
fPath:string;
implementation
end.
//Unit Threads
unit Threads;
interface
uses
Classes,Define,SysUtils;
type
TComputeThread = class(TThread)
private
{ Private declarations }
Fterminated: Boolean;
procedure MultiRead(var i,j:integer);
procedure ExclusiveWrite();
protected
procedure Execute;
override;
public
property Terminated:Boolean read Fterminated Write Fterminated;
constructor Create(create_suspended:boolean);
end;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TComputeThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TComputeThread }
constructor TComputeThread.Create(create_suspended: boolean);
begin
inherited create(create_suspended);
FreeOnTerminate:=True;
Fterminated:= Terminated;
end;
procedure TComputeThread.ExclusiveWrite;
begin
mReWsyn.begin
Write;
glo_x:=glo_x + 2;
glo_y:=glo_y + 2;
mReWsyn.EndWrite;
end;
procedure TComputeThread.Execute;
var
i,j:integer;
fpath:string;
begin
{ Place thread code here }
if not terminated then
begin
AssignFile(F,fPath);
while i <100do
begin
MultiRead(i,j);//////////////////////////////////////////////////////////////////
append(F);/////////////////////////////////该行报错/////////////////////////////
writeLn(F,inttostr(ThreadId) + ':////i:' + inttostr(i) + '////____****' + inttostr(j) + '****');
ExclusiveWrite;
end;
CloseFile(F);
end;
end;
procedure TComputeThread.MultiRead(var i,j:integer);
begin
mReWsyn.begin
Read;
i:=glo_x;
j:=glo_y;
mReWsyn.EndRead;
end;
end.
//Unit Define 中定义全局变量
unit Define;
interface
uses SysUtils;
var
glo_x,glo_y:integer;
mReWsyn:TMultiReadExclusiveWriteSynchronizer;
F:TextFile;
fPath:string;
implementation
end.
//Unit Threads
unit Threads;
interface
uses
Classes,Define,SysUtils;
type
TComputeThread = class(TThread)
private
{ Private declarations }
Fterminated: Boolean;
procedure MultiRead(var i,j:integer);
procedure ExclusiveWrite();
protected
procedure Execute;
override;
public
property Terminated:Boolean read Fterminated Write Fterminated;
constructor Create(create_suspended:boolean);
end;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TComputeThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TComputeThread }
constructor TComputeThread.Create(create_suspended: boolean);
begin
inherited create(create_suspended);
FreeOnTerminate:=True;
Fterminated:= Terminated;
end;
procedure TComputeThread.ExclusiveWrite;
begin
mReWsyn.begin
Write;
glo_x:=glo_x + 2;
glo_y:=glo_y + 2;
mReWsyn.EndWrite;
end;
procedure TComputeThread.Execute;
var
i,j:integer;
fpath:string;
begin
{ Place thread code here }
if not terminated then
begin
AssignFile(F,fPath);
while i <100do
begin
MultiRead(i,j);//////////////////////////////////////////////////////////////////
append(F);/////////////////////////////////该行报错/////////////////////////////
writeLn(F,inttostr(ThreadId) + ':////i:' + inttostr(i) + '////____****' + inttostr(j) + '****');
ExclusiveWrite;
end;
CloseFile(F);
end;
end;
procedure TComputeThread.MultiRead(var i,j:integer);
begin
mReWsyn.begin
Read;
i:=glo_x;
j:=glo_y;
mReWsyn.EndRead;
end;
end.