G
guaiguaizhang
Unregistered / Unconfirmed
GUEST, unregistred user!
我有两个线程分别对一个数组付值,另一个线程则从这个数组中取值
我定义了这样一个类,代码:
unit Unit1;
interface
uses
Classes, SysUtils, SyncObjs;
type dealwith_data = record
GPIndex : integer;
getdata : boolean;
end;
type
Tdealwith_Array = class(TCriticalSection)
private
dealwith_length : integer;
dealwith_array : array of dealwith_data;
public
procedure SetArrayLength(length_array: Integer);
//初始化数组
procedure AddArrayLength;
//增加数组长度
function GetNextIndex : integer;
//得到数据
function NewIndexIntoArray(newIndex : Integer):integer;//插入数据
end;
implementation
procedure Tdealwith_Array.SetArrayLength(length_array: Integer);
var
i : integer;
begin
dealwith_length := length_array;
SetLength(dealwith_array, dealwith_length);
for i:=0 to dealwith_length-1do
begin
dealwith_array.GPIndex := -1;
dealwith_array.getdata := false;
end;
end;
procedure Tdealwith_Array.AddArrayLength;
var
i, oldlength : integer;
begin
oldlength := dealwith_length;
dealwith_length := dealwith_length+10;
for i:=oldlength to dealwith_length-1do
begin
dealwith_array.GPIndex := -1;
dealwith_array.getdata := false;
end;
end;
Function Tdealwith_Array.GetNextIndex: Integer;
var
i : integer;
begin
try
for i:=0 to dealwith_length-1do
if dealwith_array.getdata then
break;
if dealwith_array.getdata then
begin
Result := dealwith_array.GPIndex;
dealwith_array.GPIndex := -1;
dealwith_array.getdata := false;
end
else
Result := -1;
except
Result := -1;
end;
end;
Function Tdealwith_Array.NewIndexIntoArray(newindex : integer):integer;
var
i : integer;
begin
try
Result := 0;
for i:=0 to dealwith_length-1do
if not dealwith_array.getdata then
break;
if not dealwith_array.getdata then
begin
dealwith_array.GPIndex := newindex;
dealwith_array.getdata := true;
Result := i;
end
else
if i=dealwith_length-1 then
Result := -1;
except
result := 0;
end;
end;
end.
主线成中
var
dealwith_array1 : Tdealwith_array;
procedure TForm1.FormCreate(Sender: TObject);
begin
dealwith_array1 := Tdealwith_array.create;
dealwith_array1.SetArrayLength(200);
end;
付值线程:
var
result_exec : integer;
begin
dealwith_array1.Acquire;
try
result_exec := dealwith_array1.NewIndexIntoArray(CurIndex);
if result_exec=-1 then
begin
dealwith_array1.AddArrayLength;
dealwith_array1.NewIndexIntoArray(CurIndex);
end;
finally
dealwith_array1.Release;
end;
end;
取值线程:
var
getdata : integer;
begin
dealwith_array1.Acquire;
try
getdata := dealwith_array1.GetNextIndex;
finally
dealwith_array1.Release;
end;
end;
程序运行时即报非法操作,不知怎么回事?
我定义了这样一个类,代码:
unit Unit1;
interface
uses
Classes, SysUtils, SyncObjs;
type dealwith_data = record
GPIndex : integer;
getdata : boolean;
end;
type
Tdealwith_Array = class(TCriticalSection)
private
dealwith_length : integer;
dealwith_array : array of dealwith_data;
public
procedure SetArrayLength(length_array: Integer);
//初始化数组
procedure AddArrayLength;
//增加数组长度
function GetNextIndex : integer;
//得到数据
function NewIndexIntoArray(newIndex : Integer):integer;//插入数据
end;
implementation
procedure Tdealwith_Array.SetArrayLength(length_array: Integer);
var
i : integer;
begin
dealwith_length := length_array;
SetLength(dealwith_array, dealwith_length);
for i:=0 to dealwith_length-1do
begin
dealwith_array.GPIndex := -1;
dealwith_array.getdata := false;
end;
end;
procedure Tdealwith_Array.AddArrayLength;
var
i, oldlength : integer;
begin
oldlength := dealwith_length;
dealwith_length := dealwith_length+10;
for i:=oldlength to dealwith_length-1do
begin
dealwith_array.GPIndex := -1;
dealwith_array.getdata := false;
end;
end;
Function Tdealwith_Array.GetNextIndex: Integer;
var
i : integer;
begin
try
for i:=0 to dealwith_length-1do
if dealwith_array.getdata then
break;
if dealwith_array.getdata then
begin
Result := dealwith_array.GPIndex;
dealwith_array.GPIndex := -1;
dealwith_array.getdata := false;
end
else
Result := -1;
except
Result := -1;
end;
end;
Function Tdealwith_Array.NewIndexIntoArray(newindex : integer):integer;
var
i : integer;
begin
try
Result := 0;
for i:=0 to dealwith_length-1do
if not dealwith_array.getdata then
break;
if not dealwith_array.getdata then
begin
dealwith_array.GPIndex := newindex;
dealwith_array.getdata := true;
Result := i;
end
else
if i=dealwith_length-1 then
Result := -1;
except
result := 0;
end;
end;
end.
主线成中
var
dealwith_array1 : Tdealwith_array;
procedure TForm1.FormCreate(Sender: TObject);
begin
dealwith_array1 := Tdealwith_array.create;
dealwith_array1.SetArrayLength(200);
end;
付值线程:
var
result_exec : integer;
begin
dealwith_array1.Acquire;
try
result_exec := dealwith_array1.NewIndexIntoArray(CurIndex);
if result_exec=-1 then
begin
dealwith_array1.AddArrayLength;
dealwith_array1.NewIndexIntoArray(CurIndex);
end;
finally
dealwith_array1.Release;
end;
end;
取值线程:
var
getdata : integer;
begin
dealwith_array1.Acquire;
try
getdata := dealwith_array1.GetNextIndex;
finally
dealwith_array1.Release;
end;
end;
程序运行时即报非法操作,不知怎么回事?