# Development Guidelines - [Available APIs](#section158501652121514) - [How to Develop](#section783435801510) - [Development Example](#section460018317164) - [Example Description](#section2148236125814) - [Sample Code](#section121451047155716) - [Verification](#section2742182082117) ## Available APIs

Category

API

Description

Creating or deleting a message queue

LOS_QueueCreate

Creates a message queue. The system dynamically allocates the queue space.

LOS_QueueDelete

Deletes the specified queue based on the queue ID.

Reading or writing data in a queue (without the content contained in the address)

LOS_QueueRead

Reads data in the head node of the specified queue. The data in the queue node is an address.

LOS_QueueWrite

Writes the value of the input parameter bufferAddr (buffer address) to the tail node of the specified queue.

LOS_QueueWriteHead

Writes the value of the input parameter bufferAddr (buffer address) to the head node of the specified queue.

Reading or writing in a queue (with the content contained in the address)

LOS_QueueReadCopy

Reads data from the head node of the specified queue.

LOS_QueueWriteCopy

Writes the data saved in the input parameter bufferAddr to the tail node of the specified queue.

LOS_QueueWriteHeadCopy

Writes the data saved in the input parameter bufferAddr to the head node of the specified queue.

Obtaining queue information

LOS_QueueInfoGet

Obtains information about the specified queue, including the queue ID, queue length, message node size, head node, tail node, number of readable nodes, number of writable nodes, tasks waiting for read operations, and tasks waiting for write operations.

## How to Develop 1. Call **LOS\_QueueCreate** to create a queue. The queue ID is returned when the queue is created. 2. Call **LOS\_QueueWrite** or **LOS\_QueueWriteCopy** to write messages to the queue. 3. Call **LOS\_QueueRead** or **LOS\_QueueReadCopy** to read messages from the queue. 4. Call **LOS\_QueueInfoGet** to obtain queue information. 5. Call **LOS\_QueueDelete** to delete the queue. >![](../public_sys-resources/icon-note.gif) **NOTE:** >- The maximum number of queues supported by the system is the total number of queue resources of the system, not the number of queue resources available to users. For example, if the system software timer occupies one more queue resource, the number of queue resources available to users decreases by one. >- The input parameters queue name and flags passed when a queue is created are reserved for future use. >- The input parameter **timeOut** in the queue interface function is relative time. >- **LOS\_QueueReadCopy**, **LOS\_QueueWriteCopy**, and **LOS\_QueueWriteHeadCopy** are a group of APIs that must be used together. **LOS\_QueueRead**, **LOS\_QueueWrite**, and **LOS\_QueueWriteHead** are a group of APIs that must be used together. >- As **LOS\_QueueWrite**, **LOS\_QueueWriteHead**, and **LOS\_QueueRead** are used to manage data addresses, you must ensure that the memory directed by the pointer obtained by calling **LOS\_QueueRead** is not modified or released abnormally when the queue is being read. Otherwise, unpredictable results may occur. >- **LOS\_QueueWrite**, **LOS\_QueueWriteHead**, and **LOS\_QueueRead** are called to manage data addresses, which means that the actual data read or written is pointer data. Therefore, before using these APIs, ensure that the message node size is the pointer length during queue creation, to avoid waste and read failures. ## Development Example ### Example Description Create a queue and two tasks. Enable task 1 to call the queue write API to send messages, and enable task 2 to receive messages by calling the queue read API. 1. Create task 1 and task 2 by calling **LOS\_TaskCreate**. 2. Create a message queue by calling **LOS\_QueueCreate**. 3. Enable messages to be sent in task 1 by calling **SendEntry**. 4. Enable messages to be received in task 2 by calling **RecvEntry**. 5. Delete the queue by calling **LOS\_QueueDelete**. ### Sample Code The sample code is as follows: ``` #include "los_task.h" #include "los_queue.h" static UINT32 g_queue; #define BUFFER_LEN 50 VOID SendEntry(VOID) { UINT32 ret = 0; CHAR abuf[] = "test message"; UINT32 len = sizeof(abuf); ret = LOS_QueueWriteCopy(g_queue, abuf, len, 0); if(ret != LOS_OK) { printf("send message failure, error: %x\n", ret); } } VOID RecvEntry(VOID) { UINT32 ret = 0; CHAR readBuf[BUFFER_LEN] = {0}; UINT32 readLen = BUFFER_LEN; ret = LOS_QueueReadCopy(g_queue, readBuf, &readLen, 0); if(ret != LOS_OK) { printf("recv message failure, error: %x\n", ret); } printf("recv message: %s\n", readBuf); ret = LOS_QueueDelete(g_queue); if(ret != LOS_OK) { printf("delete the queue failure, error: %x\n", ret); } printf("delete the queue success.\n"); } UINT32 ExampleQueue(VOID) { printf("start queue example.\n"); UINT32 ret = 0; UINT32 task1, task2; TSK_INIT_PARAM_S initParam = {0}; initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)SendEntry; initParam.usTaskPrio = 9; initParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; initParam.pcName = "SendQueue"; LOS_TaskLock(); ret = LOS_TaskCreate(&task1, &initParam); if(ret != LOS_OK) { printf("create task1 failed, error: %x\n", ret); return ret; } initParam.pcName = "RecvQueue"; initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)RecvEntry; initParam.usTaskPrio = 10; ret = LOS_TaskCreate(&task2, &initParam); if(ret != LOS_OK) { printf("create task2 failed, error: %x\n", ret); return ret; } ret = LOS_QueueCreate("queue", 5, &g_queue, 0, 50); if(ret != LOS_OK) { printf("create queue failure, error: %x\n", ret); } printf("create the queue success.\n"); LOS_TaskUnlock(); return ret; } ``` ### Verification The development is successful if the return result is as follows: ``` start queue example. create the queue success. recv message: test message. delete the queue success. ```