예제
작업 큐의 작업 및 완료에 사용되는 스레드를 명시적으로 제어하고 싶을 수 있습니다. 이러한 경우 *수동 작업 큐(manual task queue)*를 만들고 호출을 직접 디스패치합니다. 수동 작업 큐를 사용하는 경우, Windows 메시지 큐도 펌프해야 합니다. 다음 예에서는 수동으로 펌프되는 작업 큐를 만드는 방법을 보여 줍니다. 작업 포트와 완료 포트 모두에 대한 호출을 디스패치하는 두 개의 STL 스레드를 만듭니다.void CreatingTaskQueueWithManualThreads()
{
// Create a manual task queue.
XTaskQueueHandle queue;
HRESULT hr = XTaskQueueCreate(
XTaskQueueDispatchMode::Manual,
XTaskQueueDispatchMode::Manual,
&queue);
if (FAILED(hr))
{
printf("Creating queue failed: 0x%x\r\n", hr);
return;
}
// We create threads to pump the queue: one thread for the work port
// and one thread for the completion port.
std::thread workThread([queue]
{
// XTaskQueueDispatch returns false when there's nothing to
// dispatch. Here, we wait forever for something new to come
// in. This will return false only if the queue is being
// terminated.
while (XTaskQueueDispatch(queue, XTaskQueuePort::Work, INFINITE));
});
std::thread completionThread([queue]
{
// XTaskQueueDispatch returns false when there's nothing to
// dispatch. Here, we wait forever for something new to come
// in. This will return false only if the queue is being
// terminated.
while (XTaskQueueDispatch(queue, XTaskQueuePort::Completion, INFINITE));
});
SubmitCallbacks(queue);
// Wait a while for the callbacks to run.
Sleep(1000);
// Terminating the queue will cause a waiting XTaskQueueDispatch to return
// false.
XTaskQueueTerminate(queue, true, nullptr, nullptr);
workThread.join();
completionThread.join();
}
