> ## 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

建立工作佇列，可用來將呼叫加入佇列並分派。

## 語法

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

### 參數

*workDispatchMode*   \_In\_\
類型：[XTaskQueueDispatchMode](/zh-TW/reference/system/xtaskqueue/enums/xtaskqueuedispatchmode)

佇列之「工作」連接埠的分派模式。

*completionDispatchMode*   \_In\_\
類型：[XTaskQueueDispatchMode](/zh-TW/reference/system/xtaskqueue/enums/xtaskqueuedispatchmode)

佇列之「完成」連接埠的分派模式。

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

新建立的佇列。

### 傳回值

類型：HRESULT

HRESULT 成功或錯誤碼。

## 備註

<Note>在時間敏感執行緒上呼叫此函式並不安全。如需詳細資訊，請參閱[時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

工作佇列是參考計數物件。請呼叫 [XTaskQueueCloseHandle](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueueclosehandle) 來釋放參考。

工作佇列具有工作連接埠和完成連接埠，而工作可以加入任一連接埠的佇列。每個連接埠都可以設定自己的分派模式。

在大多數情況下，您會使用 **XTaskQueueCreate** API 建立工作佇列。不過，如果您要將多個非同步呼叫鏈結在一起，有時透過 [XTaskQueueCreateComposite](/zh-TW/reference/system/xtaskqueue/functions/xtaskqueuecreatecomposite) 建立複合佇列會很有用。假設有一個 API，其實作需要叫用另一個使用工作佇列的 API。您可能想要重新導向該 API 的完成回呼，使其不會佔用呼叫端的完成執行緒。在此情況下，您可以建立一個複合工作佇列，其工作連接埠和完成連接埠都是從另一個佇列的工作連接埠建立而成。

下列範例會建立一個工作佇列，在系統執行緒集區上分派工作回呼和完成回呼。

<Note>**SubmitCallback** 是在 [XTaskQueueSubmitCallback](/zh-TW/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](/zh-TW/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();
}
```

## 需求

**標頭：** XTaskQueue.h

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [建立工作佇列](/zh-TW/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-create-task-queue)
* [XTaskQueue 程式庫概觀](/zh-TW/build/core-features/common/async/async-libraries/async-library-xtaskqueue)
* [非同步程式設計模型](/zh-TW/build/core-features/common/async/async-programming-model)
* [非同步程式設計的設計目標和改進](/zh-TW/build/core-features/common/async/async-whitepaper)
* [時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另請參閱

[XTaskQueue 成員](/zh-TW/reference/system/xtaskqueue/xtaskqueue_members)\
[非同步程式設計模型](/zh-TW/build/core-features/common/async/async-programming-model)\
[非同步工作佇列設計](/zh-TW/build/core-features/common/async/async-task-queue-design)


## Related topics

- [XTaskQueueCreate](/zh-CN/reference/system/xtaskqueue/functions/xtaskqueuecreate.md)
- [XTaskQueueCreateComposite](/reference/system/xtaskqueue/functions/xtaskqueuecreatecomposite.md)
- [Create task queue example](/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-create-task-queue.md)
- [XTaskQueue](/reference/system/xtaskqueue/xtaskqueue_members.md)
- [XTaskQueueDispatchMode](/reference/system/xtaskqueue/enums/xtaskqueuedispatchmode.md)
