...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::interprocess::message_queue_t
// In header: <boost/interprocess/ipc/message_queue.hpp> template<typename VoidPointer> class message_queue_t { public: // types typedef VoidPointer void_pointer; typedef boost::intrusive::pointer_traits< void_pointer >::template rebind_pointer< char >::type char_ptr; typedef boost::intrusive::pointer_traits< char_ptr >::difference_type difference_type; typedef boost::container::dtl::make_unsigned< difference_type >::type size_type; // construct/copy/destruct message_queue_t(create_only_t, const char *, size_type, size_type, const permissions & = permissions()); message_queue_t(open_or_create_t, const char *, size_type, size_type, const permissions & = permissions()); message_queue_t(open_only_t, const char *); message_queue_t(create_only_t, const wchar_t *, size_type, size_type, const permissions & = permissions()); message_queue_t(open_or_create_t, const wchar_t *, size_type, size_type, const permissions & = permissions()); message_queue_t(open_only_t, const wchar_t *); ~message_queue_t(); // public member functions void send(const void *, size_type, unsigned int); bool try_send(const void *, size_type, unsigned int); template<typename TimePoint> bool timed_send(const void *, size_type, unsigned int, const TimePoint &); void receive(void *, size_type, size_type &, unsigned int &); bool try_receive(void *, size_type, size_type &, unsigned int &); template<typename TimePoint> bool timed_receive(void *, size_type, size_type &, unsigned int &, const TimePoint &); size_type get_max_msg() const; size_type get_max_msg_size() const; size_type get_num_msg() const; // public static functions static bool remove(const char *); static bool remove(const wchar_t *); };
A class that allows sending messages between processes.
message_queue_t
public
construct/copy/destructmessage_queue_t(create_only_t, const char * name, size_type max_num_msg, size_type max_msg_size, const permissions & perm = permissions());
Creates a process shared message queue with name "name". For this message queue, the maximum number of messages will be "max_num_msg" and the maximum message size will be "max_msg_size". Throws on error and if the queue was previously created.
message_queue_t(open_or_create_t, const char * name, size_type max_num_msg, size_type max_msg_size, const permissions & perm = permissions());
Opens or creates a process shared message queue with name "name". If the queue is created, the maximum number of messages will be "max_num_msg" and the maximum message size will be "max_msg_size". If queue was previously created the queue will be opened and "max_num_msg" and "max_msg_size" parameters are ignored. Throws on error.
message_queue_t(open_only_t, const char * name);
Opens a previously created process shared message queue with name "name". If the queue was not previously created or there are no free resources, throws an error.
message_queue_t(create_only_t, const wchar_t * name, size_type max_num_msg, size_type max_msg_size, const permissions & perm = permissions());
Creates a process shared message queue with name "name". For this message queue, the maximum number of messages will be "max_num_msg" and the maximum message size will be "max_msg_size". Throws on error and if the queue was previously created.
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).
message_queue_t(open_or_create_t, const wchar_t * name, size_type max_num_msg, size_type max_msg_size, const permissions & perm = permissions());
Opens or creates a process shared message queue with name "name". If the queue is created, the maximum number of messages will be "max_num_msg" and the maximum message size will be "max_msg_size". If queue was previously created the queue will be opened and "max_num_msg" and "max_msg_size" parameters are ignored. Throws on error.
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).
message_queue_t(open_only_t, const wchar_t * name);
Opens a previously created process shared message queue with name "name". If the queue was not previously created or there are no free resources, throws an error.
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).
~message_queue_t();
Destroys *this and indicates that the calling process is finished using the resource. All opened message queues are still valid after destruction. The destructor function will deallocate any system resources allocated by the system for use by this process for this resource. The resource can still be opened again calling the open constructor overload. To erase the message queue from the system use remove().
message_queue_t
public member functionsvoid send(const void * buffer, size_type buffer_size, unsigned int priority);
Sends a message stored in buffer "buffer" with size "buffer_size" in the message queue with priority "priority". If the message queue is full the sender is blocked. Throws interprocess_error on error.
bool try_send(const void * buffer, size_type buffer_size, unsigned int priority);
Sends a message stored in buffer "buffer" with size "buffer_size" through the message queue with priority "priority". If the message queue is full the sender is not blocked and returns false, otherwise returns true. Throws interprocess_error on error.
template<typename TimePoint> bool timed_send(const void * buffer, size_type buffer_size, unsigned int priority, const TimePoint & abs_time);
Sends a message stored in buffer "buffer" with size "buffer_size" in the message queue with priority "priority". If the message queue is full the sender retries until time "abs_time" is reached. Returns true if the message has been successfully sent. Returns false if timeout is reached. Throws interprocess_error on error.
void receive(void * buffer, size_type buffer_size, size_type & recvd_size, unsigned int & priority);
Receives a message from the message queue. The message is stored in buffer "buffer", which has size "buffer_size". The received message has size "recvd_size" and priority "priority". If the message queue is empty the receiver is blocked. Throws interprocess_error on error.
bool try_receive(void * buffer, size_type buffer_size, size_type & recvd_size, unsigned int & priority);
Receives a message from the message queue. The message is stored in buffer "buffer", which has size "buffer_size". The received message has size "recvd_size" and priority "priority". If the message queue is empty the receiver is not blocked and returns false, otherwise returns true. Throws interprocess_error on error.
template<typename TimePoint> bool timed_receive(void * buffer, size_type buffer_size, size_type & recvd_size, unsigned int & priority, const TimePoint & abs_time);
Receives a message from the message queue. The message is stored in buffer "buffer", which has size "buffer_size". The received message has size "recvd_size" and priority "priority". If the message queue is empty the receiver retries until time "abs_time" is reached. Returns true if the message has been successfully sent. Returns false if timeout is reached. Throws interprocess_error on error.
size_type get_max_msg() const;
Returns the maximum number of messages allowed by the queue. The message queue must be opened or created previously. Otherwise, returns 0. Never throws
size_type get_max_msg_size() const;
Returns the maximum size of message allowed by the queue. The message queue must be opened or created previously. Otherwise, returns 0. Never throws
size_type get_num_msg() const;
Returns the number of messages currently stored. Never throws
message_queue_t
public static functionsstatic bool remove(const char * name);
Removes the message queue from the system. Returns false on error. Never throws
static bool remove(const wchar_t * name);
Removes the message queue from the system. Returns false on error. Never throws
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).