Search code examples
boostvxworks

boost::interprocess::message_queue priority scheme


OS: vxWorks 7 22.09
Boost Library Version: 1.75
Protect Type: RTP
I'm working on boost::interprocess::message_queue to establish IPC between two RTPs.

Documentation Link

What's A Message Queue?
A message queue is similar to a list of messages. Threads can put messages in the queue and they can also remove messages from the queue. Each message can have also a priority so that higher priority messages are read before lower priority messages. Each message has some attributes:

A priority.
The length of the message.
The data (if length is bigger than 0).

I'm planning to use try_send API.

bool try_send(const void * buffer, size_type buffer_size, 
              unsigned int priority);

I wish to know what is the scheme used to denote higher priority and lower priority.

Is 0 - MINIMUM(unsigned int) is said to have the highest priority or 4294967295 - MAXIMUM(unsigned int) is said to have the highest priority.

The POSIX standard specifies a priority numbering scheme in which higher priorities are indicated by larger numbers. The VxWorks native numbering scheme is the reverse of this, with higher priorities indicated by smaller numbers. For example, in the VxWorks native priority numbering scheme, the highest priority task has a priority of 0.


Solution

  • The docs can be taken literally:

    What's A Message Queue?

    A message queue is similar to a list of messages. Threads can put messages in the queue and they can also remove messages from the queue. Each message can have also a priority so that higher priority messages are read before lower priority messages. Each message has some attributes:

    • A priority.
    • The length of the message.
    • The data (if length is bigger than 0).

    The highlighted sentence contains your answer: higher priority messages are read before lower priority messages

    In practice this is easily tested, at least on my linux box:

    #include <boost/interprocess/ipc/message_queue.hpp>
    #include <iostream>
    namespace bip = boost::interprocess;
    
    int main(int argc, char**) try {
        if (argc == 1) {
            // Erase previous message queue
            bip::message_queue::remove("message_queue");
    
            // Create a message_queue.
            bip::message_queue mq(bip::create_only, // only create
                                  "message_queue",  // name
                                  100,              // max message number
                                  sizeof(int)       // max message size
            );
    
            // Send 100 numbers
            for (int i = 0; i < 100; ++i) {
                mq.send(&i, sizeof(i), i % 3);
            }
            int i = 999;
            mq.send(&i, sizeof(i), 0);
        } else {
            // Open a message queue.
            bip::message_queue mq(bip::open_only, // only open
                                  "message_queue" // name
            );
    
            unsigned int                  priority;
            bip::message_queue::size_type recvd_size;
    
            // Receive 100 numbers
            for (int i = 0; i < 100; ++i) {
                int number;
                mq.receive(&number, sizeof(number), recvd_size, priority);
                std::cout << "Received: " << number << " with prio:" << priority << "\n";
    
                if (i == 999)
                    break;
            }
        }
    } catch (bip::interprocess_exception const& ex) {
        std::cout << ex.what() << std::endl;
        return 1;
    }
    

    Prints the expectable:

    Received: 2 with prio:2
    Received: 5 with prio:2
    Received: 8 with prio:2
    Received: 11 with prio:2
    ...
    Received: 1 with prio:1
    Received: 4 with prio:1
    Received: 7 with prio:1
    ...
    Received: 0 with prio:0
    ...
    Received: 96 with prio:0
    Received: 99 with prio:0
    

    enter image description here