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

# XTaskQueueRegisterMonitor

> XTaskQueueRegisterMonitor

# XTaskQueueRegisterMonitor

Registers a callback that will be invoked whenever a callback is submitted to this queue.

## Syntax

```cpp theme={null}
HRESULT XTaskQueueRegisterMonitor(  
         XTaskQueueHandle queue,  
         void* callbackContext,  
         XTaskQueueMonitorCallback* callback,  
         XTaskQueueRegistrationToken* token  
)  
```

### Parameters

*queue*   \_In\_\
Type: XTaskQueueHandle

The queue to register the submit callback to.

*callbackContext*   \_In\_opt\_\
Type: void\*

An optional context pointer to be passed to the submit callback.

*callback*   \_In\_\
Type: [XTaskQueueMonitorCallback\*](/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback)

A callback that will be invoked when a new callback is submitted to the queue.

*token*   \_Out\_\
Type: [XTaskQueueRegistrationToken\*](/reference/system/xtaskqueue/structs/xtaskqueueregistrationtoken)

A token used when calling [XTaskQueueUnregisterMonitor](/reference/system/xtaskqueue/functions/xtaskqueueunregistermonitor) to remove the callback.

### Return value

Type: HRESULT

HRESULT success or error code.

## Remarks

<Note>This function isn't safe to call on a time-sensitive thread. For more information, see [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads).</Note>

The following example shows how a task queue can be used to signal a condition variable when it has items to dispatch. An application might use this to share threads it uses for the task queue with other work in the application.

<Note>**SubmitCallback** is a helper function that is defined in the code example for the [XTaskQueueSubmitCallback](/reference/system/xtaskqueue/functions/xtaskqueuesubmitcallback) function.</Note>

```cpp theme={null}
// We will use condition variables to signal when there is something for us 
// in the queue and a bool to signal when we want the whole thing to close.
// Keep all this in a struct for convenient access
struct QueueControl
{
    std::condition_variable workActivity;
    std::mutex workMutex;
    std::condition_variable completionActivity;
    std::mutex completionMutex;
    bool terminate = false;
} queueControl;

void CALLBACK TaskQueueNewItemSubmitted(void* context, XTaskQueueHandle, XTaskQueuePort port)
{
    // A new callback has been submitted. notify the correct condition variable
    QueueControl* queueControl = static_cast<QueueControl*>(context);
    switch (port)
    {
    case XTaskQueuePort::Work:
        queueControl->workActivity.notify_all();
        break;

    case XTaskQueuePort::Completion:
        queueControl->completionActivity.notify_all();
        break;
    }
}

void CreatingTaskQueueWithManualSignaling()
{
    // 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;
    }

    // Listen to callback submitted notifications to signal
    // our condition variable.
    XTaskQueueRegistrationToken token;
    hr = XTaskQueueRegisterMonitor(
        queue, &queueControl, 
        TaskQueueNewItemSubmitted, &token);

    std::thread workThread([&]()
    {
        std::unique_lock<std::mutex> lock(queueControl.workMutex);
        while (!queueControl.terminate)
        {
            queueControl.workActivity.wait(lock);
            XTaskQueueDispatch(queue, XTaskQueuePort::Work, 0);
        }
    });

    std::thread completionThread([&]()
    {
        std::unique_lock<std::mutex> lock(queueControl.completionMutex);
        while (!queueControl.terminate)
        {
            queueControl.completionActivity.wait(lock);
            XTaskQueueDispatch(queue, XTaskQueuePort::Completion, 0);
        }
    });

    SubmitCallbacks(queue);

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

    XTaskQueueTerminate(queue, true, nullptr, nullptr);

    queueControl.terminate = true;
    queueControl.workActivity.notify_all();
    queueControl.completionActivity.notify_all();

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

## Requirements

**Header:** XTaskQueue.h

**Library:** xgameruntime.lib

**Supported platforms:** Windows, XBOX One family consoles and XBOX Series consoles

## Conceptual documentation

* [XTaskQueue library overview](/build/core-features/common/async/async-libraries/async-library-xtaskqueue)
* [Time-sensitive threads](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## See also

[XTaskQueue members](/reference/system/xtaskqueue/xtaskqueue_members)\
[Asynchronous Programming Model](/build/core-features/common/async/async-programming-model)\
[Async Task Queue Design](/build/core-features/common/async/async-task-queue-design)


## Related topics

- [XTaskQueueUnregisterMonitor](/reference/system/xtaskqueue/functions/xtaskqueueunregistermonitor.md)
- [XTaskQueueMonitorCallback](/reference/system/xtaskqueue/functions/xtaskqueuemonitorcallback.md)
- [Making async calls in the XSAPI C API](/services/xbox-services/fundamentals/xbox-services-api/live-flatc-async-patterns.md)
- [Making async calls in the PlayFab Unified SDK](/services/playfab/sdks/unified-sdk/async-model.md)
- [Making Async Calls](/services/playfab/sdks/c/async.md)
