C++ POSIX Message Queues wrapper
- C++11 (lambdas)
- header-only
- multi-threaded
- asynchronous
MQ is a header-only library. You don't need to build anything to install it. Simply add the header file to your include path and include "MQ.h":
#include "MQ.h"
The MQ library needs to be linked to the rt
and pthread
libraries, so if you save your file as main.cpp
, you can compile it by running:
g++ --std=c++11 -O3 main.cpp -lrt -lpthread
Create a server endpoint by calling the MQ constructor.
The constructor excepts an ID (std::string
) and the endpoint type:
MQ *messageQ = new MQ("example", MQ::EndpointType::Server);
Listen for incoming messages by calling the listen()
method, which will call the provided lambda when a message is received:
messageQ->listen([](std::string message){
std::cout << "Received: " << message << std::endl;
});
Send a message to the client by calling the sendMessage()
method:
messageQ->sendMessage("Hello from the server!");
Create a client endpoint by passing the appropriate type to the constructor:
MQ *client = new MQ("example", MQ::EndpointType::Client);
Send a message to the server:
client->sendMessage("Hello from the client!");
Read the response from the server:
std::cout << "Received: " << client->readMessage() << std::endl;