GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.waspscripts.com — Threading Threading¶Threading library for managing concurrent and scheduled tasks. Includes a thread pool for executing asynchronous operations and a scheduler for recurring tasks with priority queuing and built-in error handling. Credits: Developed by TazE type PThreadTask¶A pointer to a TThreadTask record. type PThreadWorker¶A pointer to a TThreadWorker record. type PThreadPool¶A pointer to a TThreadPool record. type EDebugLevel¶An enumeration defining the verbosity level for logging.
type TOnException¶A callback method that is triggered when a task execution throws an exception. Parameters:
type TOnComplete¶A callback method that is triggered when a task finishes execution. Parameters:
type TThreadTask¶Represents a single unit of work to be executed by a worker thread.
type TThreadWorker¶Represents a single worker thread within a thread pool.
type TTaskQueue¶A thread-safe, priority-based queue for managing tasks.
type TThreadPool¶Manages a collection of worker threads to execute tasks concurrently.
type TScheduledTask¶Represents a task that is scheduled to run at regular intervals.
type TThreadScheduler¶Manages and runs scheduled tasks using a dedicated thread pool.
type TThreadManager¶A high-level manager providing a simple interface to both a general-purpose thread pool and a task scheduler.
TTaskQueue.Init¶procedure TTaskQueue.Init(); Initializes a new TTaskQueue.Free¶procedure TTaskQueue.Free(); Frees the resources used by the queue, including clearing all tasks and freeing the lock. Always call this when you’re done with a queue to prevent memory leaks. TTaskQueue.Add¶procedure TTaskQueue.Add(Task: PThreadTask); Adds a Parameters:
TTaskQueue.Get¶function TTaskQueue.Get(): PThreadTask; Retrieves and removes the highest-priority task (the head) from the queue. Returns Returns:
TTaskQueue.IsEmpty¶function TTaskQueue.IsEmpty(): Boolean; Returns Returns:
TTaskQueue.Clear¶procedure TTaskQueue.Clear(); Removes and disposes of all tasks currently in the queue, resetting it to an empty state. This operation is thread-safe. TThreadWorker.Execute¶procedure TThreadWorker.Execute(); The main execution loop for a worker thread. It continuously fetches tasks from the pool’s queue and processes them. If no tasks are available, it sleeps with an exponential backoff strategy to reduce CPU usage. This method is automatically called when the worker thread starts and should not be called manually. TThreadPool.Log¶procedure TThreadPool.Log(Level: EDebugLevel; const Msg: String); Logs a message to the console if the given Parameters:
TThreadPool.GetDefaultThreadCount¶function TThreadPool.GetDefaultThreadCount(): Integer; Determines the optimal number of threads for the pool based on the system’s CPU information, clamped between Returns:
TThreadPool.Init¶procedure TThreadPool.Init(const PoolName: String; ThreadCount: Integer = 0; DebugLevel: EDebugLevel = EDebugLevel.NONE); Initializes the thread pool, creating and starting the specified number of worker threads. Parameters:
Example: var Pool: TThreadPool; Pool.Init('MyPool', 4, EDebugLevel.INFO); TThreadPool.Shutdown¶procedure TThreadPool.Shutdown(TimeoutMS: Integer = 30000); Gracefully shuts down the thread pool. It waits for busy workers to finish their current tasks up to a specified timeout before terminating all threads. Parameters:
TThreadPool.Free¶procedure TThreadPool.Free(); Frees all resources associated with the thread pool. It ensures a proper shutdown before deallocating memory. Always call this when you’re done with a thread pool to prevent memory leaks. TThreadPool.ValidateTask¶function TThreadPool.ValidateTask(const TaskID: String; Method: procedure() of object): String; Checks if a task is valid for submission. Returns an empty string on success or an error message on failure. Parameters:
Returns:
TThreadPool.ValidateTask (overload)¶function TThreadPool.ValidateTask(const TaskID: String; Proc: procedure): String; overload; Checks if a task is valid for submission. Returns an empty string on success or an error message on failure. Parameters:
Returns:
TThreadPool.HasPendingTask¶function TThreadPool.HasPendingTask(const TaskID: String): Boolean; Checks if a task with the given ID is already in the queue. Parameters:
Returns:
TThreadPool.Submit¶function TThreadPool.Submit(const TaskID: String; Method: procedure() of object; Priority: Integer = 0; AllowDuplicates: Boolean = False): Boolean; Submits a task to the pool’s queue for execution. By default, prevents duplicate task IDs. Returns Parameters:
Returns:
Example: if Pool.Submit('MyTask', @MyProcedure, 5) then WriteLn('Task submitted successfully'); TThreadPool.Submit (overload)¶function TThreadPool.Submit(const TaskID: String; Proc: procedure; Priority: Integer = 0; AllowDuplicates: Boolean = False): Boolean; overload; Submits a task to the pool’s queue for execution. By default, prevents duplicate task IDs. Returns Parameters:
Returns:
Example: if Pool.Submit('MyTask', @MyProcedure, 5) then WriteLn('Task submitted successfully'); TThreadPool.GetTask¶function TThreadPool.GetTask(): PThreadTask; Internal function used by workers to retrieve the next task from the queue. This should not be called directly by user code. Returns:
TThreadPool.ProcessTask¶procedure TThreadPool.ProcessTask(Task: PThreadTask; WorkerID: Integer); Internal function that executes a task’s method, handles exceptions, and triggers completion/exception callbacks. This should not be called directly by user code. Parameters:
TThreadPool.QueueSize¶function TThreadPool.QueueSize(): Integer; Returns the current number of tasks waiting in the queue. Returns:
TThreadPool.ActiveWorkers¶function TThreadPool.ActiveWorkers(): Integer; Returns the number of worker threads that are currently running. Returns:
TThreadPool.BusyWorkers¶function TThreadPool.BusyWorkers(): Integer; Returns the number of worker threads that are currently executing a task. Returns:
TThreadPool.Await¶procedure TThreadPool.Await(TimeoutMS: Integer = 30000); Waits until the task queue is empty and all workers are idle, or until the timeout is reached. Parameters:
TThreadPool.GetStats¶function TThreadPool.GetStats(): String; Returns a formatted string containing performance statistics for the thread pool. Returns:
TThreadPool.ResetStats¶procedure TThreadPool.ResetStats(); Resets the performance counters of the thread pool. This clears all task counts and resets the start time. TThreadScheduler.Loop¶procedure TThreadScheduler.Loop(); The main loop for the scheduler. It periodically checks all scheduled tasks and submits them to its thread pool for execution if their interval has elapsed. This method runs in a separate thread and should not be called manually. TThreadScheduler.Init¶procedure TThreadScheduler.Init(const Name: String; ThreadCount: Integer = 0; DebugLevel: EDebugLevel = EDebugLevel.NONE); Initializes the scheduler, its internal thread pool, and starts the scheduling loop. Parameters:
TThreadScheduler.Free¶procedure TThreadScheduler.Free(); Frees all resources used by the scheduler, including its thread pool and management thread. Always call this when you’re done with a scheduler to prevent memory leaks. TThreadScheduler.Schedule¶function TThreadScheduler.Schedule(const TaskID: String; Method: procedure() of object; IntervalMS: Integer = 100): Boolean; Schedules a new recurring task. Parameters:
Returns:
TThreadScheduler.Schedule (overload)¶function TThreadScheduler.Schedule(const TaskID: String; Proc: procedure; IntervalMS: Integer = 100): Boolean; overload; Schedules a new recurring task. Parameters:
Returns:
TThreadScheduler.Pause¶function TThreadScheduler.Pause(const TaskID: String): Boolean; Pauses a recurring task, preventing it from being scheduled for execution. Returns Parameters:
Returns:
TThreadScheduler.PauseAll¶procedure TThreadScheduler.PauseAll(); Pauses all currently active scheduled tasks. TThreadScheduler.Unschedule¶function TThreadScheduler.Unschedule(const TaskID: String): Boolean; Permanently removes a recurring task from the scheduler. Returns Parameters:
Returns:
TThreadScheduler.Resume¶function TThreadScheduler.Resume(const TaskID: String): Boolean; Resumes a paused recurring task. Returns Parameters:
Returns:
TThreadScheduler.GetTasks¶function TThreadScheduler.GetTasks(): TStringArray; Returns a string array of the IDs of all currently active scheduled tasks. Returns:
TThreadManager.Init¶procedure TThreadManager.Init(ThreadCount: Integer = 0; DebugLevel: EDebugLevel = EDebugLevel.NONE); Initializes the thread manager, which in turn sets up the main thread pool and the scheduler. This is the main entry point for using the library. Parameters:
Example: var Manager: TThreadManager; Manager.Init(8, EDebugLevel.INFO); TThreadManager.Free¶procedure TThreadManager.Free(); Frees all resources used by the thread manager, including its thread pool and scheduler. Always call this when you’re done with the thread manager to prevent memory leaks. TThreadManager.Submit¶function TThreadManager.Submit(const TaskID: String; Method: procedure() of object; Priority: Integer = 0; AllowDuplicates: Boolean = False): Boolean; Submits a one-off task for asynchronous execution. By default, prevents duplicate task IDs. Parameters:
Returns:
TThreadManager.Submit (overload)¶function TThreadManager.Submit(const TaskID: String; Proc: procedure; Priority: Integer = 0; AllowDuplicates: Boolean = False): Boolean; overload; Submits a one-off task for asynchronous execution. By default, prevents duplicate task IDs. Parameters:
Returns:
TThreadManager.Schedule¶function TThreadManager.Schedule(const TaskID: String; Method: procedure() of object; IntervalMS: Integer): Boolean; Schedules a recurring task to be executed at a fixed interval. Parameters:
Returns:
TThreadManager.Schedule (overload)¶function TThreadManager.Schedule(const TaskID: String; Proc: procedure; IntervalMS: Integer): Boolean; overload; Schedules a recurring task to be executed at a fixed interval. Parameters:
Returns:
TThreadManager.PauseTask¶function TThreadManager.PauseTask(const TaskID: String): Boolean; Pauses a scheduled task. Parameters:
Returns:
TThreadManager.ResumeTask¶function TThreadManager.ResumeTask(const TaskID: String): Boolean; Resumes a previously paused scheduled task. Parameters:
Returns:
TThreadManager.CancelTask¶function TThreadManager.CancelTask(const TaskID: String): Boolean; Permanently removes a scheduled task. Parameters:
Returns:
TThreadManager.SubmitBatch¶function TThreadManager.SubmitBatch(const TaskIDs: TStringArray; Methods: array of procedure() of object): Boolean; Submits a batch of one-off tasks for execution. Parameters:
Returns:
TThreadManager.SubmitBatch (overload)¶function TThreadManager.SubmitBatch(const TaskIDs: TStringArray; Procs: array of procedure): Boolean; overload; Submits a batch of one-off tasks for execution. Parameters:
Returns:
TThreadManager.Await¶procedure TThreadManager.Await(TimeoutMS: Integer = 30000); Waits for all submitted one-off tasks to complete. Parameters:
TThreadManager.Stats¶function TThreadManager.Stats(): String; Returns a string containing performance statistics for both the main pool and the scheduler. Returns:
TThreadManager.SetExceptionHandler¶procedure TThreadManager.SetExceptionHandler(Handler: TOnException); Sets a global callback to handle exceptions from any task in the manager. Parameters:
TThreadManager.SetCompletionHandler¶procedure TThreadManager.SetCompletionHandler(Handler: TOnComplete); Sets a global callback to handle the completion of any task in the manager. Parameters:
TThreadManager.SetDebugLevel¶procedure TThreadManager.SetDebugLevel(Level: EDebugLevel); Sets the logging verbosity level for the entire thread manager and its components. Parameters:
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |