任务队列
/// <author>cxg 2020-7-13</author>
/// 任务队列(线程安全)
unit tasks;
interface
uses
System.SyncObjs,
System.SysUtils, System.Generics.Collections, MsgPack;
type
TTasks = class
private
fQueue: TQueue<TMsgPack>; //队列
fCS: TCriticalSection;
public
constructor Create;
destructor Destroy; override;
procedure inQueue(task: TMsgPack); //入队
function outQueue: TMsgPack; //出队
end;
implementation
{ TTasks }
constructor TTasks.Create;
begin
fQueue := TQueue<TMsgPack>.Create;
fCS := TCriticalSection.Create;
end;
destructor TTasks.Destroy;
begin
FreeAndNil(fQueue);
FreeAndNil(fCS);
inherited;
end;
procedure TTasks.inQueue(task: TMsgPack);
begin
fCS.Enter;
fQueue.Enqueue(task);
fCS.Leave;
end;
function TTasks.outQueue: TMsgPack;
begin
fCS.Enter;
Result := fQueue.Dequeue;
fCS.Leave;
end;
end.
原文:https://www.cnblogs.com/hnxxcxg/p/13294946.html