0

I'm looking to make a thread pool. I have found a couple of examples on line, but they use TSemaphore in the SyncObjs library.

I'm using Delphi 6 and my SyncObjs doesn't include TSemaphore. I've looked around the net and can't find any source code for it.

Is there a library that works with Delphi 6 that includes TSemaphore?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Michael Vincent
  • 1,620
  • 1
  • 20
  • 46
  • `TSemaphore` is a simple wrapper around the Win32 semaphore object. You can build your own class to encapsulate that easily enough. Start with the MSDN documentation. – David Heffernan Jul 04 '14 at 14:10
  • Actually the Win32 API also contains a Thread pool implementation. You may want to use that one (or the one from OmmniThread) before you reinvent the wheel (and go through all the mistakes too. Thread pool isn't too easy to do correctly) – Daniel Jul 04 '14 at 14:18
  • @Daniel, Thread Pool API exists since Vista and even though the support for XP has gone, there's still so many machines running it. – TLama Jul 04 '14 at 14:28
  • @TLama: Thats correct. But it's still possible to use OmmniThread (I think they support XP upwards). Unless you're very experienced in multi-threading you're bound to make serious mistakes (which may even make performance worse than single-threaded execution...). And I'm not even talking about deadlocks or resource optimiztaion – Daniel Jul 04 '14 at 14:39
  • @TLama: there is a [thread pool API in XP](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686756.aspx) as well. Vista simply introduced a [newer API](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686766.aspx). – Remy Lebeau Jul 04 '14 at 14:44
  • @Remy, ah, ok. Thanks! Then I'm taking back my comments (deleting to not mislead anyone). – TLama Jul 04 '14 at 14:45
  • 1
    @Daniel, OmniThreadLibrary is for D2007 and higher. – Uwe Raabe Jul 04 '14 at 15:01
  • Seems I missed that one... Delphi 6 is pretty old. (Note: The XP Thread pool API is pretty poor and very complex. Not sure it's worth it) – Daniel Jul 04 '14 at 15:23
  • Delphi 6 is very old. I look at all the lovely stuff you guys have, like anonymous methods and future variables and weep. We're kind of stuck with Delphi 6 because many/most of our projects are done with Kylix and Delphi. If only Linux support had continued. – Michael Vincent Jul 04 '14 at 16:18

1 Answers1

7

The TSemaphore class is a simple wrapper around the Win32 semaphore API. It is very easy to create a simple wrapper in the same style. For example:

type
  TSemaphore = class
  private
    FHandle: THandle;
  public
    constructor Create(AInitialCount, AMaximumCount: Integer);
    destructor Destroy; override;
    procedure Acquire;
    function Release(AReleaseCount: Integer): Integer; overload;
    procedure Release; overload;
  end;

constructor TSemaphore.Create(AInitialCount, AMaximumCount: Integer);
begin
  inherited Create;
  FHandle := CreateSemaphore(nil, AInitialCount, AMaximumCount, nil);
  Win32Check(FHandle <> 0);
end;

destructor TSemaphore.Destroy;
begin
  if FHandle <> 0 then
    CloseHandle(FHandle);
  inherited;
end;

procedure TSemaphore.Acquire;
begin
  Win32Check(WaitForSingleObject(FHandle, INFINITE) = WAIT_OBJECT_0);
end;

function TSemaphore.Release(AReleaseCount: Integer): Integer;
begin
  Win32Check(ReleaseSemaphore(FHandle, AReleaseCount, @Result));
end;

procedure TSemaphore.Release;
begin
  Release(1);
end;

This is about as simple as can be. Starting from this you should be able to add any bells and whistles that you need.

Note that I have not tested this, so please don't blindly copy it without trying to understand it.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490