首页 > 其他 > 详细

任务队列

时间:2020-07-13 19:14:54      阅读:42      评论:0      收藏:0      [点我收藏+]

任务队列

/// <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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!