Message Queues (msgQLib.h )


Introduction

VxWorks provides message queues as a data-bearing inter-thread communication media. Two threads can share a message queue in which one can put data and the other can get data. Calls to put and get data are blocking: If a thread attempts to get data from an empty queue it is blocked. If a thread attempts to put data in a full message queue it is blocked.

Message queue creation

Message queues are created using msgQCreate(). The call returns a MSG_Q_ID type number that is used later to access the message queue. In the construction it is specified how large the message queue should be (in terms of messages), how big each message is (in terms of bytes), and some options. The example below illustrates:
void myMain()
{
 MSG_Q_ID mqId; // The id of the message queue - used for subsequent access
 ...
 // Create a message queue that can hold 10 messages, each 8 bytes long,
 // and that is accessed in prioritized order
 mqId = msgQCreate(10, 8, MSG_Q_PRIORITY);
}

Sending a message

The function msgQSend() is used to send a message. If the message queue is full, the calling task is blocked. The caller is unblocked as soon as there is room for a message in the queue (i.e., someone has used msgQReceive() on the message queue). An example:

void myMain()
{
 // The message to send
 const char msg[] = "Hello world!";
 
 // Same creation as above
 MSG_Q_ID mqID = msgQCreate(10, 8, MSG_Q_PRIORITY);
 ...
 // Send the message (8 first bytes), wait indefinately for the send to complete.
 // Send as a normal-priority mesgsage
 msgQSend(mqId, msg, 8, WAIT_FOREVER, MSG_PRI_NORMAL);
 ...
}

Receiving a message

The function msgQReceive() is used to receive a message from a message queue. If the queue is empty, the calling task is blocked. The caller is then unblocked as soon as the queue is non-empty (i.e., someone has used msgQSend() on the message queue). An example:

void myMain()
{
    // The message to send
    char recvMsg[8];
 
    // Same creation as above
    MSG_Q_ID mqID = msgQCreate(10, 8, MSG_Q_PRIORITY);
    ...
    // Receive the message (8 bytes), wait indefinately for the reception.
    msgQReceive(mqId, recvMsg, 8, WAIT_FOREVER);
    ...
}

Message queue deletion

A message queue is deleted using msgQDelete(), specifying its ID:

void myMain()
{
    MSG_Q_ID mqID = msgQCreate(10, 8, MSG_Q_PRIORITY);
    ...
    // Delete the message queue
    msgQDelete(mqId);
    ...
}