IPC Message Queues
Message queue is another commonly used inter-thread communication method. It can receive messages of variable length from threads or interrupt service routines and cache the messages in its own memory space. Other threads can also read corresponding messages from the message queue, and when the message queue is empty, the reading thread can be suspended. When a new message arrives, the suspended thread will be awakened to receive and process the message.
The main operations of message queues include: creating or opening through the mq_open() function, calling mq_send() to send a message to the message queue, calling mq_receive() to get a message from the message queue, and calling mq_unlink() to delete the message queue when it is not in use.
The POSIX message queue is mainly used for inter-process communication. The POSIX message queue of the RT-Thread operating system is mainly a package based on the RT-Thread kernel message queue, and is mainly used for communication between threads in the system. The usage is similar to the RT-Thread kernel message queue.
Each message queue corresponds to a message queue control block. Before creating a message queue, you need to define a message queue control block. The message queue control block is defined in the mqueue.h header file.
parameter | describe |
name | Message queue name |
oflag | How to open the message queue |
return | —— |
Message queue handle | success |
NULL | fail |
This function creates a new message queue or opens an existing message queue according to the name of the message queue. The optional values of Oflag are 0, O_CREAT or O_CREAT|O_EXCL. If Oflag is set to O_CREAT, a new message queue will be created. If Oflag is set to O_CREAT|O_EXCL, NULL will be returned if the message queue already exists, and a new message queue will be created if it does not exist. If Oflag is set to 0, NULL will be returned if the message queue does not exist.
parameter | describe |
name | Message queue name |
return | —— |
0 | success |
-1 | fail |
This function searches for a message queue based on the message queue name name. If found, the message queue is set to a detached state. If the hold count is 0, the message queue is deleted and the resources occupied by the message queue are released.
parameter | describe |
mqdes | Message queue handle |
return | —— |
0 | success |
-1 | fail |
When a thread terminates, the shutdown operation is performed on the message queue it occupies. This shutdown operation is performed regardless of whether the thread terminates voluntarily or involuntarily, which is equivalent to reducing the hold count of the message queue by 1. If the hold count is 0 after reduction by 1 and the message queue is in a detached state, the mqdes message queue will be deleted and the resources it occupies will be released.
parameter | describe |
mqdes | Message queue handle, cannot be NULL |
sg_ptr | Pointer to the message to be sent, cannot be NULL |
msg_len | The length of the message sent |
msg_prio | RT-Thread does not implement parameters |
return | —— |
0 | success |
-1 | fail |
This function is used to send a message to the mqdes message queue and is the encapsulation of the rt_mq_send() function. This function adds the message pointed to by msg_ptr to the mqdes message queue. The length of the message sent, msg_len, must be less than or equal to the maximum message length set when the message queue is created.
If the message queue is full, that is, the number of messages in the message queue is equal to the maximum number of messages, the thread or interrupt program that sends the message will receive an error code (-RT_EFULL).
parameter | describe |
mqdes | Message queue handle, cannot be NULL |
msg_ptr | Pointer to the message to be sent, cannot be NULL |
msg_len | The length of the message sent |
msg_prio | RT-Thread does not implement parameters |
abs_timeout | The specified waiting time is in OS ticks. |
return | —— |
0 | success |
-1 | fail |
Currently RT-Thread does not support sending messages with a specified blocking time, but the function interface has been implemented, which is equivalent to calling mq_send().
parameter | describe |
mqdes | Message queue handle, cannot be NULL |
msg_ptr | Pointer to the message to be sent, cannot be NULL |
msg_len | The length of the message sent |
msg_prio | RT-Thread does not implement parameters |
return | —— |
Message length | success |
-1 | fail |
This function removes the oldest message in the mqdes message queue from the message queue and puts the message into the memory pointed to by msg_ptr. If the message queue is empty, the thread calling the mq_receive() function will be blocked until a message is available in the message queue.
parameter | describe |
mqdes | Message queue handle, cannot be NULL |
msg_ptr | Pointer to the message to be sent, cannot be NULL |
msg_len | The length of the message sent |
msg_prio | RT-Thread does not implement parameters |
abs_timeout | The specified waiting time is in OS ticks. |
return | —— |
Message length | success |
-1 | fail |
The difference between this function and the mq_receive() function is that if the message queue is empty, the thread will be blocked for the abs_timeout duration. After the timeout, the function directly returns - 1, and the thread will be awakened from the blocked state to the ready state.
This program will create three threads, thread 1 receives messages from the message queue, and thread 2 and thread 3 send messages to the message queue.
Last updated