Clean up task queue example
This topic provides an example of how to clean up a task queue when it's no longer needed to avoid leaking resources. However, a task queue can't just immediately be released. It must first be terminated, flushed to ensure that anything enqueued to it has been run, and then released.
The terminate call can be set to wait or return immediately. If a callback isn't waiting, it can optionally be specified to be notified when the termination completely finishes. If there are any manual ports, they must continue to be dispatched until the dispatch function returns.
XTaskQueueTerminate(taskQueue, false, nullptr, nullptr);
bool result;
do
{
result = XTaskQueueDispatch(taskQueue, XTaskQueuePort::Completion, INFINITE);
} while (result == true);
XTaskQueueCloseHandle(taskQueue);
This example terminates the task queue but doesn't wait by using the
XTaskQueueTerminate wait parameter.
The terminate
call adds one callback to each port to detect when the queue
is emptied. To account for this and any previously enqueued callbacks,
dispatch
is continually called until it returns false
.