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?
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.