Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:20:55+00:00 2026-06-11T08:20:55+00:00

I am implementing a pool of objects in Delphi. I need to synchronize the

  • 0

I am implementing a pool of objects in Delphi. I need to synchronize the threads to get the objects from the pool.

Thread Code:

uClientQueryPool.CLIENT_POOL_GUARD.Acquire();
QueryClient := QUERY_POOL.GetClient();
uClientQueryPool.CLIENT_POOL_GUARD.Release;

Pool Code:

var
   CLIENT_POOL_GUARD: TCriticalSection;

type
   TClientQueryPool = class
public
   function GetClient(): TQueryClient;
end;

The CLIENT_POOL_GUARD is a unit variable. The pool is working well, but can I use “uClientQueryPool.CLIENT_POOL_GUARD.Acquire();” and “uClientQueryPool.CLIENT_POOL_GUARD.Release;” inside the GetClient method?

Like this:

function TClientQueryPool.GetClient: TQueryClient;
begin
    CLIENT_POOL_GUARD.Acquire();
    ...
    CLIENT_POOL_GUARD.Release;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:20:56+00:00Added an answer on June 11, 2026 at 8:20 am

    Moving the lock inside the get/pop/whatever method is just fine, as is making the CriticalSection instance a private member of the pool class. Use the same CS in the release() call that pushes the objects back onto the pool.

    Been doing this for decades, usually with TObjectQueue as the pool queue, a CS to protect it and a semaphore to count the pool contents and something for requesting threads to block on if the pool empties temporarily.

    Don’t know where that ‘double acquire’ thread came from. Either the lock is inside the pool class, or outside. I really can’t imagine why anyone would code up both!

    Example classes:

    First, thread-safe P-C queue, for holding the pooled objects:

    unit tinySemaphoreQueue;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Classes,syncObjs,contnrs;
    
    
    type
    
    pObject=^Tobject;
    
    
    TsemaphoreMailbox=class(TobjectQueue)
    private
      countSema:Thandle;
    protected
      access:TcriticalSection;
    public
      property semaHandle:Thandle read countSema;
      constructor create; virtual;
      procedure push(aObject:Tobject); virtual;
      function pop(pResObject:pObject;timeout:DWORD):boolean;  virtual;
    end;
    
    
    implementation
    
    { TsemaphoreMailbox }
    
    constructor TsemaphoreMailbox.create;
    begin
      inherited Create;
      access:=TcriticalSection.create;
      countSema:=createSemaphore(nil,0,maxInt,nil);
    end;
    
    function TsemaphoreMailbox.pop(pResObject: pObject;
      timeout: DWORD): boolean;
    begin // wait for a unit from the semaphore
      result:=(WAIT_OBJECT_0=waitForSingleObject(countSema,timeout));
      if result then // if a unit was supplied before the timeout,
      begin
        access.acquire;
        try
          pResObject^:=inherited pop; // get an object from the queue
        finally
          access.release;
        end;
      end;
    end;
    
    procedure TsemaphoreMailbox.push(aObject: Tobject);
    begin
      access.acquire;
      try
        inherited push(aObject); // shove the object onto the queue
      finally
        access.release;
      end;
      releaseSemaphore(countSema,1,nil); // release one unit to semaphore
    end;
    
    end.
    

    then object pool:

    unit tinyObjectPool;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Classes,syncObjs,contnrs,
      tinySemaphoreQueue;
    
    type
      TobjectPool=class;
    
      TpooledObject=class(TObject)
      private
        FmyPool:TObjectPool;
      protected
        Fparameter:TObject;
      public
        procedure release;
        constructor create(parameter:TObject); virtual;
      end;
    
      TpooledObjectClass=class of TpooledObject;
    
      TobjectPool=class(TsemaphoreMailbox)
      private
        Fparameter:TObject;
        function getPoolLevel: integer;
      public
        property poolLevel:integer read getPoolLevel;
        constructor create(poolDepth:integer;
          pooledObjectClass:TpooledObjectClass;parameter:TObject); reintroduce; virtual;
      end;
    
    implementation
    
    { TobjectPool }
    
    constructor TobjectPool.create(poolDepth: integer;
      pooledObjectClass: TpooledObjectClass;parameter:TObject);
    var objectCount:integer;
        thisObject:TpooledObject;
    begin
      inherited create;
      Fparameter:=parameter; // a user parameter passed to all objects
      for objectCount:=0 to poolDepth-1 do // fill up the pool with objects
      begin
        thisObject:=pooledObjectClass.create(parameter);
        thisObject.FmyPool:=self;
        inherited push(thisObject);
      end;
    end;
    
    function TobjectPool.getPoolLevel: integer;
    begin
      access.acquire;
      result:=inherited count;
      access.release;
    end;
    
    
    
    { TpooledObject }
    
    constructor TpooledObject.create(parameter: TObject);
    begin
      inherited create;
      Fparameter:=parameter;
    end;
    
    procedure TpooledObject.release;
    begin
      FmyPool.push(self);
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a thread pool. The work required by each thread is 1-10
I am implementing a simple thread pool mechanism for my ubuntu server (for my
implementing publishActivity in PHP using the REST API using this code: $activity = array(
When implementing Serializable interface, in Java, we need to set it's serialVersionUID long. Is
When implementing the Strategy Pattern, where does one put the code that determines which
Im implementing a pool billiard game in Java and it all works fine. It
I'm implementing a memory pool in C for a real time application. A container
Is there a known algorithm for implementing a connection pool? If not what are
I am having problems implementing my PHP/MySQL code into a jgrowl. If you are
What is the good design pattern for implementing one connection (or generally resource) pool?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.