信号量TSemaphore

信号量TSemaphore

uses syncobjs;

  TSemaphore = class(THandleObject)
{$IF Defined(LINUX) or Defined(ANDROID)}
  private
    FSem: sem_t;
{$ENDIF LINUX or ANDROID}
{$IFDEF MACOS}
  private
    FSem: dispatch_semaphore_t;
{$ENDIF MACOS}
  public
    constructor Create(UseCOMWait: Boolean = False); overload;
    constructor Create(SemaphoreAttributes: PSecurityAttributes; AInitialCount, AMaximumCount: Integer; const Name: string; UseCOMWait: Boolean = False); overload;
    constructor Create(DesiredAccess: Cardinal; InheritHandle: Boolean; const Name: string; UseCOMWait: Boolean = False); overload;
{$IFDEF POSIX}
    destructor Destroy; override;
    function WaitFor(Timeout: Cardinal = INFINITE): TWaitResult; override;
{$ENDIF POSIX}
    procedure Acquire; override;
    procedure Release; overload; override;
    function Release(AReleaseCount: Integer): Integer; reintroduce; overload;
  end;

  可以看出,此类是支持跨平台的,不知始于哪个DELPHI版本,反正DELPHI7是没有这个类的。

var MySemaphore: TSemaphore;
MySemaphore := TSemaphore.Create(nil, StrToInt(Edit1.Text), 5, '');
if MySemaphore.WaitFor(INFINITE) = wrSignaled then
begin
......
end;
MySemaphore.free;

  

posted @ 2020-12-10 08:35  delphi中间件  阅读(336)  评论(0编辑  收藏  举报