|
virtual void DILIGENT_CALL_TYPE | EnqueueTask (IAsyncTask *pTask, IAsyncTask **ppPrerequisites=nullptr, Uint32 NumPrerequisites=0)=0 |
| Enqueues asynchronous task for execution.
|
|
virtual bool DILIGENT_CALL_TYPE | ReprioritizeTask (IAsyncTask *pTask)=0 |
| Reprioritizes the task in the queue.
|
|
virtual void DILIGENT_CALL_TYPE | ReprioritizeAllTasks ()=0 |
| Reprioritizes all tasks in the queue.
|
|
virtual bool DILIGENT_CALL_TYPE | RemoveTask (IAsyncTask *pTask)=0 |
| Removes the task from the queue, if possible.
|
|
virtual void DILIGENT_CALL_TYPE | WaitForAllTasks ()=0 |
| Waits until all tasks in the queue are finished.
|
|
virtual Uint32 DILIGENT_CALL_TYPE | GetQueueSize ()=0 |
| Returns the current queue size.
|
|
virtual Uint32 DILIGENT_CALL_TYPE | GetRunningTaskCount () const =0 |
| Returns the number of currently running tasks.
|
|
virtual void DILIGENT_CALL_TYPE | StopThreads ()=0 |
| Stops all worker threads.
|
|
virtual bool DILIGENT_CALL_TYPE | ProcessTask (Uint32 ThreadId, bool WaitForTask)=0 |
| Manually processes the next task from the queue.
|
|
virtual void DILIGENT_CALL_TYPE | QueryInterface (const INTERFACE_ID &IID, IObject **ppInterface)=0 |
| Queries the specific interface.
|
|
virtual ReferenceCounterValueType DILIGENT_CALL_TYPE | AddRef ()=0 |
| Increments the number of strong references by 1.
|
|
virtual ReferenceCounterValueType DILIGENT_CALL_TYPE | Release ()=0 |
|
virtual IReferenceCounters *DILIGENT_CALL_TYPE | GetReferenceCounters () const =0 |
|
virtual bool DILIGENT_CALL_TYPE Diligent::IThreadPool::ProcessTask |
( |
Uint32 | ThreadId, |
|
|
bool | WaitForTask ) |
|
pure virtual |
Manually processes the next task from the queue.
- Parameters
-
[in] | ThreadId | - Id of the thread that is running this task. |
[in] | WaitForTask | - whether the function should wait for the next task:
- if true, the function will block the thread until the next task is retrieved from the queue and processed.
- if false, the function will return immediately if there are no tasks in the queue.
|
- Returns
- Whether there are more tasks to process. The calling thread must keep calling the function until it returns false.
This method allows an application to implement its own threading strategy. A thread pool may be created with zero threads, and the application may call ProcessTask() method from its own threads.
An application must keep calling the method until it returns false. If there are unhandled tasks in the queue and the application stops processing them, the thread pool will hang up.
An example of handling the tasks is shown below:
// Initialization
auto pThreadPool = CreateThreadPool(ThreadPoolCreateInfo{0});
std::vector<std::thread> WorkerThreads(4);
for (Uint32 i PURE; i < WorkerThreads.size(); ++i)
{
WorkerThreads[i] = std::thread{
[&ThreadPool = *pThreadPool, i] //
{
while (ThreadPool.ProcessTask(i, true))
{
}
}};
}
// Enqueue async tasks
pThreadPool->WaitForAllTasks();
// Stop all threads in the pool
pThreadPool->StopThreads();
// Cleanup (must be done after all threads are stopped)
for (auto& Thread : WorkerThreads)
{
Thread.join();
}