I copied this code from the windows samples
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action)
{
while (action->Status == AsyncStatus::Started)
{
// Main Loop
}
});
m_renderLoopWorker = ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);
but have experienced some unreproducible lag sometimes (although maybe its from the gpu).
On the other hand
WorkItemOptions::TimeSliced
The work items should be run simultaneously with other work items sharing a processor.
doesn't sound like a high performance option.
WorkItemOptions::None
The work item should be run when the thread pool has an available worker thread.
Where you would want to use WorkItemOptions::TimeSliced
vs WorkItemOptions::None
?
Is it ever advisable to use CreathThread
over running a task on the thread pool for persistent work.
CodePudding user response:
WorkItemOptions::TimeSliced => preemptive multitasking
WorkItemOptions::None => cooperative multitasking
When do you want to use each one... difficult to say.
If you use None and all the threads in the thread pool are currently used, your task wont run until a thread finishes its job.
With TimeSliced each task is allowed a time slice, when the time is up, your task is paused and the thread switch to another task. This way, if you have 100 work items, but only 10 thread, all work items will progress, little by little, but 10x slower.
If you need to update something regularly, lets say a progress bar, you would rather use TimeSliced.
It is perfectly acceptable to use CreateThread for a long task. A render loop fit that description. This way you have your own thread to yourself to do whatever you want. Even though at the OS level, there is preemptive multitasking anyway, otherwise if your processor had only 2 cores, and you ran 3 threads, the 3rd thread would hang.
The main point of thread pools is to avoid creating new threads for every little task you want to do, because it incurs an overhead.