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

# XTaskQueueCreate

> XTaskQueueCreate

# XTaskQueueCreate

创建任务队列，该队列可用于将调用排队并调度。

## Syntax

```cpp theme={null}
HRESULT XTaskQueueCreate(  
         XTaskQueueDispatchMode workDispatchMode,  
         XTaskQueueDispatchMode completionDispatchMode,  
         XTaskQueueHandle* queue  
)  
```

### Parameters

*workDispatchMode*   \_In\_\
类型：[XTaskQueueDispatchMode](/reference/system/xtaskqueue/enums/xtaskqueuedispatchmode)

队列“工作”端口的调度模式。

*completionDispatchMode*   \_In\_\
类型：[XTaskQueueDispatchMode](/reference/system/xtaskqueue/enums/xtaskqueuedispatchmode)

队列“完成”端口的调度模式。

*queue*   \_Out\_\
类型：XTaskQueueHandle\*

新创建的队列。

### Return value

类型：HRESULT

HRESULT 成功或错误代码。

## Remarks

<Note>此函数在时间敏感线程上调用不安全。有关详细信息，请参阅[时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

任务队列是引用计数对象。通过调用 [XTaskQueueCloseHandle](/reference/system/xtaskqueue/functions/xtaskqueueclosehandle) 释放引用。

任务队列有工作端口和完成端口，任务可以排队到任一端口。每个端口都可以配置自己的调度模式。

对于大多数场景，可使用 **XTaskQueueCreate** API 创建任务队列。但是，如果要将多个异步调用链接在一起，则通过 [XTaskQueueCreateComposite](/reference/system/xtaskqueue/functions/xtaskqueuecreatecomposite) 创建复合队列有时会很有用。考虑一个 API，其实现需要调用另一个使用任务队列的 API。你可能希望重定向 API 的完成回调，以便它不会占用调用方的完成线程。在这种情况下，你可以创建一个复合任务队列，其工作端口和完成端口是从另一个队列的工作端口构建的。

以下示例创建一个任务队列，该队列在系统线程池上调度工作和完成回调。

<Note>**SubmitCallback** 是在 [XTaskQueueSubmitCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback) 函数的代码示例中定义的帮助程序函数。</Note>

```cpp theme={null}
void CreatingTaskQueue()
{
    XTaskQueueHandle queue;
    HRESULT hr = XTaskQueueCreate(XTaskQueueDispatchMode::ThreadPool, XTaskQueueDispatchMode::ThreadPool, &queue);
    if (FAILED(hr))
    {
        printf("Creating queue failed: 0x%x\r\n", hr);
        return;
    }

    SubmitCallbacks(queue);

    // Wait a while for the callbacks to run
    Sleep(1000);

    XTaskQueueTerminate(queue, true, nullptr, nullptr);
}
```

以下示例创建一个手动抽取的任务队列。它创建两个 STL 线程，分别为工作端口和完成端口调度调用。

<Note>**SubmitCallback** 是在 [XTaskQueueSubmitCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback) 函数的代码示例中定义的帮助程序函数。</Note>

```cpp theme={null}
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 for the work port
    // and one for the completion port.
    std::thread workThread([queue]
    {
        while (XTaskQueueDispatch(queue, XTaskQueuePort::Work, INFINITE));
    });

    std::thread completionThread([queue]
    {
        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 DispatchTaskQueue to return
    // false.  
    XTaskQueueTerminate(queue, true, nullptr, nullptr);

    workThread.join();
    completionThread.join();
}
```

## Requirements

**头文件：** XTaskQueue.h

**库：** xgameruntime.lib

**受支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## Conceptual documentation

* [创建任务队列](/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-create-task-queue)
* [XTaskQueue 库概述](/build/core-features/common/async/async-libraries/async-library-xtaskqueue)
* [异步编程模型](/build/core-features/common/async/async-programming-model)
* [异步编程设计目标和改进](/build/core-features/common/async/async-whitepaper)
* [时间敏感线程](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## See also

[XTaskQueue 成员](/reference/system/xtaskqueue/xtaskqueue_members)\
[异步编程模型](/build/core-features/common/async/async-programming-model)\
[异步任务队列设计](/build/core-features/common/async/async-task-queue-design)


## Related topics

- [XTaskQueueCreateComposite](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuecreatecomposite.md)
- [创建任务队列示例](/zh-CN/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-create-task-queue.md)
- [XTaskQueue](/zh-CN/reference/system/xtaskqueue/xtaskqueue_members.md)
- [XTaskQueueDispatchMode](/zh-CN/reference/system/xtaskqueue/enums/xtaskqueuedispatchmode.md)
- [在 PlayFab 统一 SDK 中进行异步调用](/zh-CN/services/playfab/sdks/unified-sdk/async-model.md)
