---
# System prepended metadata

title: Ch4 Queue Management
tags: [FreeRTOS]

---

# Ch4 Queue Management
###### tags: `FreeRTOS`

>致謝
>https://www.freertos.org/index.html

## xQueueCreate

**QueueHandle_t  xQueueCreate( UBaseType_t uxQueueLength,UBaseType_t uxItemSize );**
Creates a new queue and returns a handle by which the queue can be referenced. configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h, for this RTOS API function to be available.

Each queue requires RAM that is used to hold the queue state, and to hold the items that are contained in the queue.
* If a queue is created using xQueueCreate() then the required RAM is automatically allocated from the FreeRTOS heap.
* If a queue is created using xQueueCreateStatic() then the RAM is provided by the application writer, which results in a greater number of parameters, but allows the RAM to be statically allocated at compile time.

### Parameters
#### uxQueueLength :
>The maximum number of items the queue can hold at any one time.
#### uxItemSize : 
>The size, in bytes, required to hold each item in the queue.

**Items are queued by copy, not by reference**, so this is the number of bytes that will be copied for each queued item. Each item in the queue must be the same size.

### Returns
If the queue is created successfully then a handle to the created queue is returned. 
If the memory required to create the queue could not be allocated then NULL is returned.

### Example usage
```c=
struct AMessage
{
    char ucMessageID;
    char ucData[ 20 ];
};

void vATask( void *pvParameters )
{
    QueueHandle_t xQueue1, xQueue2;

    /* Create a queue capable of containing 10 unsigned long values. */
    xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );

    if( xQueue1 == NULL )
    {
        /* Queue was not created and must not be used. */
    }

    /* Create a queue capable of containing 10 pointers to AMessage
    structures.  These are to be queued by pointers as they are
    relatively large structures. */
    xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );

    if( xQueue2 == NULL )
    {
        /* Queue was not created and must not be used. */
    }

    /* ... Rest of task code. */
 }
```