> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dispatch task queue

> Shows how to spawn a dedicated thread that calls XTaskQueueDispatch on the completion port in a loop, so callbacks run until the task queue is terminated.

This topic provides an example of how to dispatch a task queue. Dispatching a task queue is necessary for each [manual](https://docs.xbox.com/reference/system/xtaskqueue/enums/xtaskqueuedispatchmode.md) port. How and
when the ports are [dispatched](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueuedispatch.md) determines the threaded and
concurrent behavior of the enqueued callbacks.

```c++ theme={null}
std::thread dispatherThread = std::thread(
    [taskQueue]()
    {
        while (true)
        {
            bool processed = XTaskQueueDispatch(taskQueue,
                XTaskQueuePort::Completion, INFINITE);

            if (!processed)
            {
                // The task queue has been terminated, so return.
                break;
            }
        }
    });
```

This example shows an extra thread being spawned to constantly dispatch
the completion port of the passed-in task queue until it's
terminated.

## Reference API documentation

* [XTaskQueue (API contents)](https://docs.xbox.com/reference/system/xtaskqueue/xtaskqueue_members.md)
  * Functions
    * [xtaskqueuedispatch](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueuedispatch.md)

## See also

[XTaskQueue library overview](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xtaskqueue.md)

[Create task queue (example)](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-create-task-queue.md)

[Clean up task queue (example)](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-cleanup-task-queue.md)

[Designing the task queue](https://docs.xbox.com/build/core-features/common/async/async-task-queue-design.md)

[XTaskQueue](https://docs.xbox.com/reference/system/xtaskqueue/xtaskqueue_members.md)


## Related topics

- [XTaskQueueDispatch](https://docs.xbox.com/reference/system/xtaskqueue/functions/xtaskqueuedispatch.md)
- [Create task queue](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-create-task-queue.md)
- [Clean up task queue](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-cleanup-task-queue.md)
- [XTaskQueue library overview](https://docs.xbox.com/build/core-features/common/async/async-libraries/async-library-xtaskqueue.md)
- [XAsync libraries for asynchronous task programming](https://docs.xbox.com/build/core-features/common/async/async-libraries/index.md)
