Exchange messages between threads in a FIFO-like operation.
More...
Message passing is another basic communication model between threads. In the message passing model, one thread sends data explicitly, while another thread receives it. The operation is more like some kind of I/O rather than a direct access to information to be shared. In CMSIS-RTOS, this mechanism is called s message queue. The data is passed from one thread to another in a FIFO-like operation. Using message queue functions, you can control, send, receive, or wait for messages. The data to be passed can be of integer or pointer type:
CMSIS-RTOS Message Queue
Compared to a Memory Pool, message queues are less efficient in general, but solve a broader range of problems. Sometimes, threads do not have a common address space or the use of shared memory raises problems, such as mutual exclusion.
- Note
- Refer to Message Queue Configuration for RTX5 configuration options.
Working with Message Queues
Follow these steps to create and use a message queue:
- Setup the message queue:
- Then, create the message queue in a thread:
- Fill the message queue with data:
- From the receiving thread access the data using:
struct osMessageQueueAttr_t |
Attributes structure for message queue.
Data Fields |
const char * |
name |
name of the message queue |
uint32_t |
attr_bits |
attribute bits |
void * |
cb_mem |
memory for control block |
uint32_t |
cb_size |
size of provided memory for control block |
void * |
mq_mem |
memory for data storage |
uint32_t |
mq_size |
size of provided memory for data storage |
- Parameters
-
[in] | msg_count | maximum number of messages in queue. |
[in] | msg_size | maximum message size in bytes. |
[in] | attr | message queue attributes; NULL: default values. |
- Returns
- message queue ID for reference by other functions or NULL in case of error.
- Parameters
-
- Returns
- name as NULL terminated string.
- Parameters
-
[in] | mq_id | message queue ID obtained by osMessageQueueNew. |
[in] | msg_ptr | pointer to buffer with message to put into a queue. |
[in] | msg_prio | message priority. |
[in] | timeout | Timeout Value or 0 in case of no time-out. |
- Returns
- status code that indicates the execution status of the function.
Code Example:
void Thread_MsgQueue1 (void *argument);
void Thread_MsgQueue2 (void *argument);
#define MSGQUEUE_OBJECTS 16 // number of Message Queue Objects
typedef struct {
uint8_t Buf[32];
uint8_t Idx;
} MSGQUEUE_OBJ_t;
int Init_MsgQueue (void) {
if (!mid_MsgQueue) {
;
}
tid_Thread_MsgQueue1 =
osThreadNew (Thread_MsgQueue1, NULL, NULL);
if (!tid_Thread_MsgQueue1) return(-1);
tid_Thread_MsgQueue2 =
osThreadNew (Thread_MsgQueue2, NULL, NULL);
if (!tid_Thread_MsgQueue2) return(-1);
return(0);
}
void Thread_MsgQueue1 (void *argument) {
MSGQUEUE_OBJ_t pMsg = 0;
while (1) {
;
pMsg->Buf[0] = 0x55;
pMsg->Idx = 0;
}
}
void Thread_MsgQueue2 (void *argument) {
MSGQUEUE_OBJ_t pMsg = 0;
while (1) {
;
;
}
}
}
- Parameters
-
[in] | mq_id | message queue ID obtained by osMessageQueueNew. |
[out] | msg_ptr | pointer to buffer for message to get from a queue. |
[out] | msg_prio | pointer to buffer for message priority or NULL. |
[in] | timeout | Timeout Value or 0 in case of no time-out. |
- Returns
- status code that indicates the execution status of the function.
- Parameters
-
- Returns
- maximum number of messages.
- Parameters
-
- Returns
- maximum message size in bytes.
- Parameters
-
- Returns
- number of queued messages.
- Parameters
-
- Returns
- number of available slots for messages.
- Parameters
-
- Returns
- status code that indicates the execution status of the function.
- Parameters
-
- Returns
- status code that indicates the execution status of the function.