Indy 9 | Content | Index | Reference

TIdThreadMgr
Hierarchy, Properties, Methods, See Also, Unit: IdThreadMgr
Specifies the base class for the Indy thread management framework.

    TIdThreadMgr = class(TIdBaseComponent)

Unit
IdThreadMgr
Description
TIdThreadMgr is the ancestor class for the thread manager classes in Indy, such as TIdThreadMgrDefault and TIdThreadMgrPool, and provides a thread management framework.

TIdThreadMgr defines properties and methods needed to allocate and release threads, monitor active threads, terminate a list of running threads, safeguard thread operations, and determine the thread class used by the thread manager to create new thread instances.

Instances of a TIdThreadMgr descendant can be assigned to the TIdTCPServer.ThreadMgr property to provide the functionality specific to the thread manager.

Descendants of TIdThreadMgr must implement the virtual abstract methods GetThread and ReleaseThread, and use some mechanism to protect resources while maintaining the list of active threads during these operations.

Use TerminateThreads to notify the ActiveThreads list that all TIdThread instances should close their socket connections, release the thread with RelaseThread, and remove the thread from ActiveThreads.

Assign an instance of TIdThreadClass to ThreadClass before using the thread manager to allocate new threads.

TIdThreadMgr.ActiveThreads
TIdThreadMgr, See Also
Active threads for the thread manager.

    property ActiveThreads: TThreadList;

Description
ActiveThreads is a read-only TList property that contains active threads instances. ActiveThreads is initialized in the Create constructor, and freed in the Destroy destructor.

TIdThreadMgr does maintain the list of active threads. Descendants of TIdThreadMgr must implement the virtual abstract methods GetThread and ReleaseThread, and maintain ActiveThreads during these operations. The thread manager should use ReleaseThread to protect resources while maintaining the list of active threads.

Use TerminateThreads to notify the ActiveThreads list that all thread instances should close their socket connections, release the thread with ReleaseThread, and remove the thread from ActiveThreads.

TIdThreadMgr.ThreadClass
TIdThreadMgr, See Also
Class type for new threads.

    property ThreadClass: TIdThreadClass;

Description
ThreadClass is a TIdThreadClass property used to determine the type of thread instances created by the TIdThreadMgr. Assign an instance of TIdThreadClass to ThreadClass before using the thread manager to allocate new threads using CreateNewThread.

TIdThreadMgr.ThreadPriority
TIdThreadMgr, See Also
Indicates the thread priority to use for new threads created by the thread manager.

    property ThreadPriority: TIdThreadPriority;

Description
ThreadPriority is a TIdThreadPriority property that indicates the thread priority to be assigned to new thread instances created in CreateNewThread. The default value for ThreadPriority is tpNormal.

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

    constructor Create(AOwner: TComponent); override;

Parameters
AOwner: TComponent
Component that owns the object instance.
Description
Create relies on the inherited Create constructor to initialize the object instance. Create also initializes the read-only property ActiveThreads. Destroy is responsible for freeing ActiveThreads.

TIdThreadMgr.CreateNewThread
TIdThreadMgr, See Also
Handles creation of new thread instances.

    function CreateNewThread: TIdThread; virtual;

Return Value
TIdThread - The new thread instance.
Description
CreateNewThread is a virtual TIdThread function in TIdThreadMgr that handles creation of new thread instances.

CreateNewThread uses ThreadClass to create a new thread instance for the thread manager. If ThreadClass is not assigned, an EIdThreadClassNotSpecified exception is raised, and the return value of CreateNewThread will be Nil.

CreateNewThread is used by TIdThreadMgr descendants to provide the new thread instance needed in GetThread.

TIdThreadMgr.Destroy
TIdThreadMgr, See Also
Destructor for the object instance.

    destructor Destroy; override;

Description
Destroy is the destructor for the object instance. Destroy will free the property ActiveThreads, and set the value to Nil prior to calling the inherited Destroy destructor.

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

    function GetThread: TIdThread; virtual; abstract;

Return Value
TIdThread - The new thread instance.
Description
GetThread is a virtual abstract TIdThread function that requests a new thread for the thread manager. GetThread is not implemented in TIdThreadMgr. Descendants of TIdThreadMgr must implement GetThread to satisfy the requirements for the specific thread manager. GetThread can get a thread instance using CreateNewThread, re-use a thread from a thread pool, or use another thread creation scheme provided in the TIdThreadMgr descendant.

GetThread should use the critical section Lock, or other mechanism, to protect the ActiveThreads list while adding the new thread instance.

A thread instance can be freed using ReleaseThread. ReleaseThread is used in TerminateThreads.

TIdThreadMgr.ReleaseThread
TIdThreadMgr, See Also
Terminate and free a thread instance.

    procedure ReleaseThread(AThread: TIdThread); virtual; abstract;

Parameters
AThread: TIdThread
Thread to be released.
Description
ReleaseThread is a virtual abstract procedure that instructs the TIdThreadMgr to terminate and free a thread instance.

ReleaseThread is not implemented in TIdThreadMgr. Descendants of TIdThreadMgr must implement ReleaseThread to satisfy the requirements for the specific thread manager. ReleaseThread can force the thread to Terminate, WaitFor the thread to complete execution, and Free the thread instance. ReleaseThread may also implement other functionality as required in the TIdThreadMgr descendant.

ReleaseThread should use the critical section Lock, or other mechanism, to protect the ActiveThreads list while removing the thread instance.

ReleaseThread is used in TerminateThreads.

TIdThreadMgr.TerminateThreads
TIdThreadMgr, See Also
Closes, releases, and frees threads in a list.

    procedure TerminateThreads; virtual;

Parameters
Threads
List of threads to terminate.
Description
TerminateThreads is a procedure that forces a list of TIdThread instances to close their socket connections, releases the threads, and remove the threads from the active thread list.

If the thread list contains TIdPeerThread instances, TerminateThreads will call TIdPeerThread.Connection.Disconnect to close the socket connection.

For each TIdThread instance in the thread list, TerminateThreads will call ReleaseThread.

The terminated thread instance will be removed from the Thread list.

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