Indy 9
|
TIdThreadMgr = class(TIdBaseComponent)
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.
property ActiveThreads: TThreadList;
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.
property ThreadClass: TIdThreadClass;
property ThreadPriority: TIdThreadPriority;
constructor Create(AOwner: TComponent); override;
function CreateNewThread: TIdThread; virtual;
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.
destructor Destroy; override;
function GetThread: TIdThread; virtual; abstract;
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.
procedure ReleaseThread(AThread: TIdThread); virtual; abstract;
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.
procedure TerminateThreads; virtual;
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.