关于同步与互斥的问题(100分)

  • 主题发起人 主题发起人 guaiguaizhang
  • 开始时间 开始时间
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;

程序运行时即报非法操作,不知怎么回事?
 
引入信號量實現線程同步與互斥。
 
我想知道这样写有什么问题
 
看看《Delphi 5开发人员指南》第十一章,有这个例子,永远的Delphi有下,网址:
http://fdelphi.myrice.com/delphidoc.html
 
TCriticalSection
 
线程是从TCriticalSection继承的???????
应该是从TThread继承吧。
 
thx1180,你有没有搞错?我说的是临界区对象,你扯什么thread?
 
Sorry,是我看错了。
 
用TCriticalSection临界区对象,效率比Mutex高
 
其实TMultiReadExclusiveWriteSynchronizer更好,但我自己也搞不明白怎么用。
 
我也有类似问题,用AB两个线程实现生者者与消费者问题。A向队列生产数据,并通过信号量告知B可以进入队列取数据处理。大概的结构如下:
procedure AThread.execute
begin
while not Terminateddo
begin
if waitforSingleObject(hsem_mutex,INFINITE)=wait_object_0 then
begin
向队列中生产数据
end;
ReleaseSemaphore(hSem_mutex,1,nil);
ReleaseSemaphore(hSem_consume,1,nil);
end;
end;
procedure BThread.execute
begin
if waitForSingleObject(hSem_consume,INFINITE)=wait_object_0 then
begin
if waitforSingleObject(hsem_mutex,INFINITE)=wait_object_0 then
begin
向队列中取出数据
end;
ReleaseSemaphore(hSem_mutex,1,nil);
end;
end;
在主程序中
procedure button1click(sender:tobject);
begin
创建信号量
AThread.create(false);
BThread.create(false);
end;
问题是这样写以后A线程运行正常,而B线程除了再创建的时候运行一下取一次数以外不再运行,如果把B线程的创建放到A的EXECUTE中,即如下所示:
procedure AThread.execute
begin
while not Terminateddo
begin
if waitforSingleObject(hsem_mutex,INFINITE)=wait_object_0 then
begin
向队列中生产数据
end;
ReleaseSemaphore(hSem_mutex,1,nil);
Bthread.create(false);
ReleaseSemaphore(hSem_consume,1,nil);
end;
end;
则可以运行。
有没有哪位大侠做过类似的程序帮帮我,指点一二。
 
后退
顶部