扩展Delphi的线程同步对象(1)

2016-01-29 14:15 3 1 收藏

扩展Delphi的线程同步对象(1),扩展Delphi的线程同步对象(1)

【 tulaoshi.com - Delphi 】

 

  
  在编写多线程应用程序时,最重要的是控制好线程间的同步资源访问,以保证线程的安全运行。Win 32 API提供了一组同步对象,如:信号灯(Semaphore)、互斥(Mutex)、临界区(CriticalSection)和事件(Event)等,用来解决这个问题。

  Delphi分别将事件对象和临界区对象封装为Tevent对象和TcritialSection对象,使得这两个对象的使用简单且方便。但是如果在Delphi程序中要使用信号灯或互斥等对象就必须借助于复杂的Win32 API函数,这对那些不熟悉Win32 API函数的编程人员来说很不方便。因此,笔者用Delphi构造了两个类,对信号灯和互斥对象进行了封装(分别为TSemaphore和TMutex),希望对广大Delphi编程人员有所帮助。

  一、类的构造
  我们先对Win32 API的信号灯对象和互斥对象进行抽象,构造一个父类THandleObjectEx,然后由这个父类派生出两个子类Tsemphore和Tmutex。

  类的源代码如下:

  unit SyncobjsEx;

  interface

  uses Windows,Messages,SysUtils,Classes,Syncobjs;

  type

   THandleObjectEx = class(THandleObject)

  // THandleObjectEx为互斥类和信号灯类的父类

   protected

   FHandle: THandle;

   FLastError: Integer;

   public

   destructor Destroy; override;

   procedure Release;override;

   function WaitFor(Timeout: DWORD): TWaitResult;

   property LastError:Integer read FLastError;

   property Handle: THandle read FHandle;

   end;

   TMutex = class(THandleObjectEx)//互斥类

   public

   constructor Create(MutexAttributes: PSecurityAttributes; InitialOwner: Boolean;const Name:string);

   procedure Release; override;

   end;

   TSemaphore = class(THandleObjectEx)

  //信号灯类

  public

  constructor Create(SemaphoreAttributes: PSecurityAttributes;InitialCount:Integer;MaximumCount: integer; const Name: string);

  procedure Release(ReleaseCount: Integer=1;PreviousCount:Pointer=nil);overload;

   end;

  implementation [next]

  { THandleObjectEx }//父类的实现

  destructor THandleObjectEx.Destroy;

  begin

   Windows.CloseHandle(FHandle);

   inherited Destroy;

  end;

  procedure THandleObjectEx.Release;

  begin

  end;

  function THandleObjectEx.WaitFor(Timeout: DWORD): TWaitResult;

  //等待函数,参数为等待时间

  begin

  case WaitForSingleObject(Handle, Timeout) of

  WAIT_ABANDONED: Result := wrAbandoned;

  //无信号

  WAIT_OBJECT_0: Result := wrSignaled;

  //有信号

  WAIT_TIMEOUT: Result := wrTimeout;//超时

  WAIT_FAILED://失败

   begin

   Result := wrError;

   FLastError := GetLastError;

   end;

   else

   Result := wrError;

   end;

  end;

  { TSemaphore }//信号灯类的实现

  constructor TSemaphore.Create(SemaphoreAttributes: PSecurityAttributes;

   InitialCount, MaximumCount: integer; const Name: string);//信号灯类的构造函数

  begin

  FHandle := CreateSemaphore

  (SemaphoreAttributes,InitialCount,

  MaximumCount,PChar(Name));

  //四个参数分别为:安全属性、初始信号灯计数、最大信号灯计数、信号灯名字

  end;

  procedure TSemaphore.Release(ReleaseCount: Integer=1; PreviousCount: Pointer=nil);

  //信号灯类的Release方法,每执行一次按指定量增加信号灯计数

  begin

   Windows.ReleaseSemaphore(FHandle, ReleaseCount, PreviousCount);

  end;

  { TMutex }//互斥类的实现

  constructor TMutex.Create(MutexAttributes: PSecurityAttributes;

  InitialOwner: Boolean; const Name: string);

  //互斥类的构造函数

  begin

   FHandle := CreateMutex(MutexAttributes, InitialOwner, PChar(Name));

  end;

  procedure TMutex.Release;//互斥类的Release方法,用来释放对互斥对象的

来源:https://www.tulaoshi.com/n/20160129/1492859.html

延伸阅读
什么是线程同步? 当使用多个线程来访问同一个数据时,非常容易出现线程安全问题(比如多个线程都在操作同一数据导致数据不一致),所以我们用同步机制来解决这些问题。 实现同步机制有两个方法 : 1。同步代码块: synchronized(同一个数据){} 同一个数据:就是N条线程同时访问一个数据。 2。 同步方法: public synchronized 数据返回...
Delphi的编程语言是以Pascal为基础的。Pascal语言具有可读性好、编写容易的特点,这使得它很适合作为基础的开发语言。同时,使用编译器创建的应用程序只生成单个可执行文件(.EXE),正是这种结合,使得Pascal成为Delphi这种先进开发环境的编程语言。 本章中,我们将讨论Object Pascal的主要特点,并讲解如何在事件处理过程和其他应用程...
/**  * pTitle: 线程同步/p  * pDescription: 通过使用同步锁实现对共享数据的操作/p  * pCopyright: Copyright (c) 2003/p  * pFilename: SyThreadDemo.java/p  * @version 1.0  */ /**  *br类说明:主程序  *br功能描述:构造两个线程,并启动它们  */ public class SyThreadDemo {  ...
标签: Delphi
  ----Windows95是Microsoft公司的第一个真正的多任务操作系统。在每一时刻可以有多个进程同时工作,而每一个进程又包含有多个线程。但只有一个处理器的计算机不可能真正地“同时”执行多个线程,而是操作系统把时间分成若干个时间片,然后把一个个时间片分配给每一个线程。 ----一个执行了的程序就是一个进程,一个进程则至少有一个...
标签: ASP
  Response对象非常重要,而且有许多特性。我们将关注其最常用的一些功能。可用于 80%场合的20%的功能。 response.write response.write的变体 <%= %它可使ASP方便地插入HTML中 response.end which 有效地中止代码 response.redirect 调用其它页面 下面是一个实例,用response.write发送信息到浏览器。同时用到了dateadd,一个 内...