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

# 方法: 手動タスク キューの作成

> 方法: 手動タスク キューの作成

## 例

タスク キューのワークおよび完了に使用されるスレッドを明示的に制御したい場合があるかもしれません。そのような場合は、*手動タスク キュー* を作成し、呼び出しを自身でディスパッチします。手動タスク キューを使用する場合は、Windows メッセージ キューもポンプ処理するようにする必要があります。

次の例は、手動でポンプ処理するタスク キューを作成する方法を示しています。ワーク ポートと完了ポートの両方の呼び出しをディスパッチする 2 つの STL スレッドを作成します。

```c++ 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 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();  
}  
```

## 関連項目

[タスク キューの設計](/build/core-features/common/async/async-task-queue-design)


## Related topics

- [方法: 複合タスク キューの作成](/ja-jp/build/core-features/common/async/async-task-queue-design-howto/creating-composite-task-queue.md)
- [方法: スレッド プール タスク キューの作成](/ja-jp/build/core-features/common/async/async-task-queue-design-howto/creating-thread-pool-task-queue.md)
- [非同期タスク キューの設計](/ja-jp/build/core-features/common/async/async-task-queue-design.md)
- [タスク キューの作成例](/ja-jp/build/core-features/common/async/async-libraries/async-library-xtaskqueue-example-create-task-queue.md)
- [方法: タスク キュー ウェイターの使用](/ja-jp/build/core-features/common/async/async-task-queue-design-howto/using-task-queue-waiter.md)
