能力值:
( LV4,RANK:45 )
|
-
-
2 楼
郁闷死了
找了队列的代码整合原先的代码修改了一下 还是出错
看来还是发代码上来 到底什么地方的问题
有想研究的自己去看下,顺便帮我看看怎么会出错
我无奈死了。
搞了四五天,诶。
密码:
kanxue
|
能力值:
( LV4,RANK:45 )
|
-
-
3 楼
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
ceshi=10;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
mmo1: TMemo;
mmo2: TMemo;
btn3: TButton;
btn4: TButton;
procedure FormCreate(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn4Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
//缓冲区对象,包含同步句柄和缓冲区
PDb = ^TDb;
TDB = class
private
FCap: integer;
Flist: TList;
FSycHandle: Thandle;
public
constructor Create(const pCap: Integer);overload;
public
FItemCount: integer; //测试适用
destructor Destroy; override;
function IsEmpty: Boolean;
function IsFULL: Boolean;
function Pop: Pointer;
procedure Push(const pObect: pointer);
property SycHandle: Thandle read FSycHandle;
end;
type
TDBB = class
private
//读取缓冲区临界区
//FPopCritical: TRTLCriticalSection;
//写入缓冲区临界区
//FPushCritical: TRTLCriticalSection;
FBufferA: TDB;
FbufferB: TDB;
FPushA: PDB;
FPushB: PDB;
FPopA: PDB;
FPopB: PDB;
function ChangeBuffer(const pQueueBuffer: PDb): pDb;
public
constructor Create(const pCap: Integer);overload;
destructor Destroy; override;
function Popup: Pointer;
function Push(const pObject: pointer): Boolean;
//
procedure Flush;
//获得缓冲区元素数
function GetALLCount: Integer;
//缓冲区是否为空
function ISEmpty: Boolean;
end;
type
Tshiwu=packed record
c:Integer;
m:Integer;
end;
type
Tthread3 = class(Tthread)
protected
procedure Execute;override;
end;
type
Tthread5 = class(Tthread)
protected
procedure Execute;override;
end;
var
Form1: TForm1;
bcunzai:Boolean;
astopnow:Boolean;
autoattnow:Boolean;
xiaofeione:Tthread3;
xiaofeitwo:Tthread5;
shiwuc:Tshiwu;
dq:TDBB;
implementation
{$R *.dfm}
constructor TDB.Create(const pCap: Integer);
begin
inherited create;
FCap := pCap;
FItemCount := 0;
FSycHandle := CreateEvent(nil, false, false, nil);
Flist := TList.Create;
end;
destructor TDB.Destroy;
begin
Flist.Clear;
Flist.Free;
CloseHandle(FSycHandle);
inherited;
end;
function TDB.IsEmpty: Boolean;
begin
Result := Flist.Count = 0;
end;
function TDB.IsFULL: Boolean;
begin
Result := Flist.count >= FCap;
if result then
begin
FItemCount := 10;
end;
end;
function TDB.Pop: Pointer;
begin
if Flist.Count = 0 then
begin
result := nil;
exit;
end;
Result := Flist.Items[0];
Flist.Delete(0);
dec(FItemCount);
end;
procedure TDB.Push(const pObect: pointer);
begin
Flist.Add(pObect);
inc(FItemCount)
end;
constructor TDBB.Create(const pCap: Integer);
begin
inherited Create;
FBufferA := TDB.Create(pCap);
FBufferB := TDB.Create(pCap);
FPushA := @FBufferA;
FPushB := @FbufferB;
FPopA := @FBufferA;
FPopB := @FbufferB;
//InitializeCriticalSection(FPopCritical);
//InitializeCriticalSection(FPushCritical);
end;
function TDBB.Popup: Pointer;
begin
//EnterCriticalSection(FPopCritical);
//try
if FPopA.IsEmpty then
begin
//释放当前读取缓冲区
SetEvent(FPopA.SycHandle);
//form1.mmo1.Lines.Add('通知 ::' + inttostr(integer(FPopA)) + ':' + inttostr(FPopA.SycHandle));
// 得到当前读取缓冲区
FPopB := ChangeBuffer(FPopA);
//form1.mmo1.Lines.Add('Pop ::' + inttostr(integer(FPopB)) + ':' + inttostr(FPopB.SycHandle));
//接管当前读取缓冲区的拥有权
WaitForSingleObject(FPopB.SycHandle, INFINITE);
// 接管读取缓冲区
FPopA := FPopB;
end;
result := FPopA.Pop;
//finally
// LeaveCriticalSection(FPopCritical);
//end;
end;
function TDBB.Push(const pObject: pointer): Boolean;
begin
//EnterCriticalSection(FPushCritical);
//try
if FPushB.IsFULL then
begin
//释放当前写入缓冲区
SetEvent(FPushB.SycHandle);
//form1.Memo1.Lines.Add('通知 ::' + inttostr(integer(FPushb)) + ':' + inttostr(FPushb.SycHandle));
//得到当前写入取缓冲区
FPushA := ChangeBuffer(FPushB);
//form1.Memo1.Lines.Add('Push ::' + inttostr(integer(FPusha)) + ':' + inttostr(FPusha.SycHandle));
//接管当前写入缓冲区的拥有权
WaitForSingleObject(FPushA.SycHandle, INFINITE);
//接管写入缓冲区
FPushB := FPushA;
end;
FPushB.Push(pObject);
result := true;
//finally
// LeaveCriticalSection(FPushCritical);
//end;
end;
destructor TDBB.Destroy;
begin
FBufferA.Free;
FBufferB.Free;
//DeleteCriticalSection(FPopCritical);
//DeleteCriticalSection(FPushCritical);
inherited;
end;
function TDBB.ChangeBuffer(const pQueueBuffer: PDb): pDb;
begin
if pQueueBuffer = @FBufferA then
begin
result := @FBufferB;
end;
if pQueueBuffer = @FBufferB then
begin
result := @FBufferA;
end;
end;
procedure TDBB.Flush;
begin
SetEvent(FBufferA.SycHandle);
SetEvent(FBufferB.SycHandle);
end;
function TDBB.GetALLCount: Integer;
begin
Result := FBufferA.FItemCount + FBufferA.FItemCount;
end;
function TDBB.ISEmpty: Boolean;
begin
Result := FBufferA.IsEmpty and FBufferB.IsEmpty;
end;
procedure eat(var shiwucm:integer);
//var
begin
Randomize;
if shiwucm=0 then
begin
shiwucm:=0;
Form1.mmo1.lines.add('事物m被消费:目前剩余'+inttostr(shiwucm));
end else
begin
shiwucm:=shiwucm-random(10);
Form1.mmo1.lines.add('事物m被消费:目前剩余'+inttostr(shiwucm));
if (shiwucm<0) or (shiwucm=0)then
begin
shiwucm:=0;
Form1.mmo1.lines.add('事物m被消费:目前剩余0');
end;
end;
end;
procedure jianshao(var shiwucc:integer);
//var
begin
Randomize;
if shiwucc=0 then
begin
shiwucc:=0;
end else
begin
shiwucc:=shiwucc-random(10)-15;
Form1.mmo2.lines.add('事物c被消费:目前剩余'+inttostr(shiwucc));
if (shiwucc<0) or (shiwucc=0)then
begin
shiwucc:=0;
Form1.mmo2.lines.add('事物c被消费:目前剩余0');
end;
end;
end;
procedure Tthread3.Execute;
begin
//repeat
//dq.Create();
repeat
if (shiwuc.m<>0)then
begin
eat(shiwuc.m); //消费物品一
end else
begin
if dq.ISEmpty()=False then //这里判断队列是否为空
begin
shiwuc.m:=integer(dq.Popup()); //这里清理队列
end;
end;
if bcunzai=false then
begin
xiaofeitwo:=Tthread5.Create(False);
end;
Sleep(50);
jianshao(shiwuc.c);
Sleep(400);
until astopnow=False;//
// dq.Destroy;
//end;
end;
procedure Tthread5.Execute;
begin
bcunzai:=True;
autoattnow:=true;
Randomize;
repeat
Sleep(150);
// shiwuctwo:=quanjushiwuc;
if shiwuc.c=0 then //这里C消费消费完毕之后 重新生产 但是会产生冲突 如果两个都消费完就没冲突 如果两个都消费完这样无效率
begin
shiwuc.m:=50+random(50);
shiwuc.c:=430+random(30);
dq.Push(@shiwuc.m); //这里把数据加入队列
Form1.mmo2.lines.add('事物m产生:'+inttostr(shiwuc.m));
Form1.mmo2.lines.add('事物c产生:'+inttostr(shiwuc.c));
end;
jianshao(shiwuc.c);
Sleep(300);
until autoattnow=False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
bcunzai:=False;
astopnow:=true;
shiwuc.m:=0;
shiwuc.c:=0;
autoattnow:=False;
//dq.Create();
end;
procedure TForm1.btn3Click(Sender: TObject);
begin
astopnow:=False;
xiaofeione.Suspend;
xiaofeione.DoTerminate;
// dq.Destroy;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
xiaofeione:=Tthread3.Create(False);
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
xiaofeitwo:=Tthread5.Create(False);
end;
procedure TForm1.btn4Click(Sender: TObject);
begin
autoattnow:=False;
xiaofeitwo.Suspend;
xiaofeitwo.DoTerminate;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
//dq.Destroy;
end;
end.
这个是代码 我的编译老是提示莫名奇妙的错误
郁闷死了
|
能力值:
( LV2,RANK:10 )
|
-
-
4 楼
代码混乱,逻辑更乱
|
能力值:
( LV4,RANK:45 )
|
-
-
5 楼
帮忙看看 谢谢
|
能力值:
( LV2,RANK:10 )
|
-
-
6 楼
要是一两句话的事也就帮忙了,这个也不是多难的事情,关键是有些东西你还没理解透,自己找一下资料吧,其实还是很多的
|
能力值:
( LV4,RANK:45 )
|
-
-
7 楼
网络上delphi 队列的资料真是凤毛鳞角。我查了基本上都没详细一点的东西
你有空就帮忙看看吧 关键其实就那几句 初始化 push pop 什么的
|
能力值:
( LV4,RANK:45 )
|
-
-
8 楼
看来数据结构这个还是有点意思,之前就没想到这么麻烦,现在郁闷了
|
能力值:
( LV4,RANK:45 )
|
-
-
9 楼
已经搞定了 而且方法超级简单 郁闷啊
|
|
|