Indy 9 | Content | Index | Reference

TIdThreadMgrPool
Hierarchy, Properties, Methods, See Also, Unit: IdThreadMgrPool
Provides thread management using a pool of TIdThread instances.

    TIdThreadMgrPool = class(TIdThreadMgr)

Unit
IdThreadMgrPool
Description
TIdThreadMgrPool is a thread manager that utilizes the thread management framework defined in TIdThreadMgr. TIdThreadMgrPool provides thread management using a pool of TIdThread instances. Threads are created by the thread manager, when necessary, or retrieved from the thread pool. TIdThreadMgrPool also keeps a list of active threads.

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.

TIdThreadMgrPool.PoolSize
TIdThreadMgrPool, See Also
Indicates the maximum size of the thread pool.

    property PoolSize: Integer;

Description
PoolSize is an Integer property used to indicate the maximum number of thread instances that can be stored in ThreadPool.

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.

TIdThreadMgrPool.Create
TIdThreadMgrPool, See Also
Create is the constructor for the object instance.

    constructor Create(AOwner: TComponent); override;

Parameters
AOwner: TComponent
Owner of the object instance.
Description
Create is the constructor for the object instance. Create relies on the inherited Create constructor to initialize the object instance, but also creates the thread list used in the thread pool.

TIdThreadMgrPool.Destroy
TIdThreadMgrPool, See Also
Destroy is the destructor for the object instance.

    destructor Destroy; override;

Description
Destroy is the destructor for the object instance. Destroy will free all threads in thread pool, locking the thread list to protect resources, and free the thread pool prior to calling the inherited Destroy destructor.

TIdThreadMgrPool.GetThread
TIdThreadMgrPool, See Also
Requests a new thread for the thread manager.

    function GetThread: TIdThread; override;

Return Value
TIdThread - The new thread instance.
Description
GetThread is an implementation of the inherited abstract TIdThread function that requests a new thread for the thread manager. GetThread tries to satisfy new thread requests by retrieving an existing thread instance from ThreadPool. <-- While GetThread searches for a pooled thread, it also frees and deletes any Terminated threads. The pooled thread instance is removed from ThreadPool. -->

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.

TIdThreadMgrPool.ReleaseThread
TIdThreadMgrPool, See Also
Remove a thread from the active thread list.

    procedure ReleaseThread(AThread: TIdThread); override;

Parameters
AThread: TIdThread
Thread to be released.
Description
ReleaseThread implements the inherited abstract procedure that instructs the thread manager to remove AThread from the list of active threads in ActiveThreads.

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.

TIdThreadMgrPool.TerminateThreads
TIdThreadMgrPool
Terminates all running threads and returns them to the thread pool.

    procedure TerminateThreads; override;

Parameters
TerminateWaitTime
Milliseconds before a timeout occurs during thread termination.
Description
TerminateThreads is an overridden procedure in TIdThreadMgrPool that allows the pooled thread manager to terminate all running threads, and return them to the pool for the thread manager. TerminateThreads calles the inherited method to halt execution of all threads using the timeout value specified in TerminateWaitTime.

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.

Created with Doc-O-Matic 2 donated to Project JEDI. Commercial license available from the Doc-O-Matic site.