Indy 9
|
TIdThreadMgrPool = class(TIdThreadMgr)
The thread pool can have a maximum size to conserve system resources.
TIdThreadMgrPool uses Lock to protect thread manager resources in GetThread and ReleaseThread.
TIdThreadMgrPool implements the inherited abstract methods GetThread and ReleaseThread. GetThread provides a thread instance for the thread manager. ReleaseThread handles freeing and releasing a thread instance, or returning the instance to the thread pool.
Use TerminateThreads to notify all TIdThread instances to close their socket connection, release the thread, and remove the thread from ActiveThreads.
Assign an instance of TIdThreadClass to ThreadClass before using the thread manager to allocate new threads.
To use TIdThreadMgrPool with TIdTCPServer, create an instance of TIdThreadMgrPool, and assign the object reference to TIdTCPServer.ThreadMgr. If you are using protocols which do not close the socket connection after each request, you may wish to consider using the TIdThreadMgrDefault thread manager. After closing a connection with TIdThreadMgrDefault, the thread and all associated data is freed. When you are using TIdThreadMgrPool, the threads is stored in the Pool and when the server needs new thread it is not created but is taken from the Pool.
property PoolSize: Integer;
PoolSize is used by ReleaseThread to determine if a thread should be returned to ThreadPool, or if the thread should Terminate and Free. PoolSize is updated in ReleaseThread when a thread finishes executing, and it is returned to ThreadPool.
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetThread: TIdThread; override;
If there are no existing threads in ThreadPool, GetThread can create a new thread instance using CreateNewThread. The new thread instance will have the StopMode value smSuspend. The thread instance will be added to ActiveThreads and returned as the result of GetThread.
GetThread locks the ThreadPool while extracting a thread for reuse.
A thread instance can be freed using ReleaseThread. A list of threads can be terminated and freed using TerminateThreads.
procedure ReleaseThread(AThread: TIdThread); override;
If the thread has not Terminated, and ThreadPool does not exceed PoolSize, the thread instance will be Stopped and returned to ThreadPool.
If AThread is the current Terminated thread of execution, it is allowed to complete with Terminate. If AThread is not the current thread of execution, AThread.TerminateAndWaitFor is called and the thread is Freed.
ReleaseThread locks and unlocks the ThreadPool list to protect resources while handling the thread instance.
procedure TerminateThreads; override;
TerminateThreads extends the ancestor class by locking the internal TThreadList for the TIdThreads in the pool to Start the thread and remove it from the locked thread list. THe locked TTHreadList is unlocked prioer to exiting from the method.