The Message Queue (later abbreviated to MQ or Message Queuing) is an implementation in the boost library that encapsulates interprocess communication, and processes or threads on the same machine can communicate through Message Queuing. Messages in Message Queuing consist of three parts: priority, message length, and message data. The thing to note here is that MQ simply copies the data that will be sent in memory, so when we send a complex structure or object, we need to serialize it and send it, and the receiver receives it to deserialize it, which means we're going toa definition distinguishes a message (that is, a custom network communication protocol). in MQ, we can use three modes to send and receive messages:
- Block: When sending a message, if the message queue is full, the sending interface will block until the queue is not full. When a message is received, if the queue is empty, the receiving interface is blocked until the queue is not empty.
- Timeout: The user can customize the time-out period, when the time-out expires, then the sending interface or the receiving interface will return, regardless of the queue full or empty
- Try: Can return immediately when the queue is empty or full
MQ uses named shared memory to implement interprocess communication. Shared memory in other words, the user can specify a name to create a piece of shared memory, and then open the shared memory like a file, and the same process can open the shared memory according to that name, so that one process writes to the shared memory, and the other process can read from the shared memory. The read and write of the two processes here involves a synchronization problem. Other than thatwhen creating an MQ, we need to specify the maximum number of messages for MQ and the maximum size of the message. [CPP]View Plaincopyprint
Create a message_queue. If the queue
exists throws an exception
Message_queue MQ
(create_only //only Create
,"Message_queue" //name
, //max message number
, //max message size
);
Using Boost::interprocess;
Creates or opens a message_queue. If the queue
Does not exist creates it, otherwise opens it.
Message number and size are ignored if the queue
Is opened
Message_queue MQ
(Open_or_create //open or create
,"Message_queue" //name
, //max message number
, //max message size
);
Using Boost::interprocess;
Opens a message_queue. If the queue
Does not exist throws an exception.
Message_queue MQ
(open_only //only Open
,"Message_queue" //name
);
using message_queue:: Remove (Next, let's look at an example of a producer and a message using Message Queuing. The first process is made as a producer and the second process is consumer. producer Process:[CPP]View Plaincopyprint
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
Using namespace boost::interprocess;
int main ()
{
try{
//erase Previous Message Queue
Message_queue::remove ("Message_queue");
//create a message_queue.
Message_queue MQ
(create_only //only Create
,"Message_queue" //name
, //max message number
,sizeof (int) //max message size
);
//send Numbers
For (int i = 0; i <; ++i) {
Mq.send (&i, sizeof (i), 0);
}
}
catch (Interprocess_exception &ex) {
Std::cout << ex.what () << Std::endl;
return 1;
}
return 0;
}
Consumer processes:
[CPP]View Plaincopyprint
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
Using namespace boost::interprocess;
int main ()
{
try{
//open a message queue.
Message_queue MQ
(open_only //only Create
,"Message_queue" //name
);
unsigned int priority;
Message_queue::size_type recvd_size;
//receive Numbers
For (int i = 0; i <; ++i) {
int number;
Mq.receive (&number, sizeof (number), recvd_size, priority);
if (number! = I | | recvd_size! = sizeof (number))
return 1;
}
}
catch (Interprocess_exception &ex) {
Message_queue::remove ("Message_queue");
Std::cout << ex.what () << Std::endl;
return 1;
}
Message_queue::remove ("Message_queue");
return 0;
}
Detailed message Queue in the Boost library.