消息队列的核心作用其实很简单,一个或多个线程往一个队列后面堆数据,另外的一个线程从队列前面取数据处理,基本操作也只有两个,一个发,一个收,但是传输的数据格式任意不限。一般的消息队列库可能只支持传基本的数据结构,大型的数据只能使用指针,不方便调试,有损代码的易读性:
来源于一个开源项目https://github.com/khuttun/PolyM
非常感谢khuttun的无私贡献
下面是描述:
PolyM
PolyM is a very simple C++ message queue intended for inter-thread communication. There are three major requirement driving the design of PolyM:
- The design should be simple and lightweight
- The design should support delivering any kind of data as the message payload
- There should be no copies made of the message payload data when passing the message through the queue
PolyM (Polymorphic Message Queue) fulfills these requirements by usage of C++ move semantics.
Moving Messages Through the Queue
When a message with payload data (PolyM::DataMsg<PayloadType>) is created, the payload is allocated from the heap. When the message is put to the queue, it is moved there with so called “virtual move constructor”. The virtual move constructor is what enables the polymorphism of the message queue: any message type derived from the base PolyM::Msg type can be moved to the queue. When a message is read from the queue, it is moved out from the queue to be the responsibility of the receiver.
All the message types are movable, but not copyable. Once the message is put to the queue, the sender cannot access the message data anymore. A message always has a single owner, and on send-receive scenario, the ownership is transferred
Sender -> Queue -> Receiver
Messaging Patterns
PolyM supports two kinds of messaging patterns:
- One way
- Messages are put to the queue with
PolyM::Queue::put().put()will return immediately. - Messages are read from the queue with
PolyM::Queue::get().get()will block until there is at least one message available in the queue (or until timeout occurs, if specified).
- Messages are put to the queue with
- Request-response
- A request is made with
PolyM::Queue::request().request()will block until a response is given. The request message can be read from the queue just as any other message withget(). - A response is given with
PolyM::Queue::respondTo().respondTo()will return immediately.
- A request is made with
以下是测试代码:
Tester.hpp
#ifndef TESTER_HPP
#define TESTER_HPP
#include <cstdlib>
#include <iostream>
#include <functional>
#include <utility>Tester.hpp
#include <string>
#include <vector>
// This file contains an implementation for *very* simple testing framework.
// Use these macros in your test functions to test for various things.
// The test program will print an error message and exit if the tests fail.
/** Test that boolean condition a is true */
#define TEST(a) test((a), #a, __FILE__, __LINE__);
/** Test that a == b */
#define TEST_EQUALS(a, b) testEquals((a), (b), __FILE__, __LINE__);
/** Test that a != b */
#define TEST_NOT_EQUALS(a, b) testNotEquals((a), (b), __FILE__, __LINE__);
/** Test that a < b */
#define TEST_LESS_THAN(a, b) testLessThan((a), (b), __FILE__, __LINE__);
/** Fail the test with error message msg */
#define TEST_FAIL(msg) testFail((msg), __FILE__, __LINE__);
void test(bool a, const char* exp, const char* file, int line)
{
if (!a)
{
std::cout << file << ":" << line << ": TEST FAILED: " << exp << std::endl;
exit(1);
}
}
template <class C> void testEquals(C a, C b, const char* file, int line)
{
if (a != b)
{
std::cout << file << ":" << line << ": TEST FAILED: " << a << " == " << b << std::endl;
exit(1);
}
}
template <class C> void testNotEquals(C a, C b, const char* file, int line)
{
if (a == b)
{
std::cout << file << ":" << line << ": TEST FAILED: " << a << " != " << b << std::endl;
exit(1);
}
}
template <class C> void testLessThan(C a, C b, const char* file, int line)
{
if (a >= b)
{
std::cout << file << ":" << line << ": TEST FAILED: " << a << " < " << b << std::endl;
exit(1);
}
}
void testFail(const char* msg, const char* file, int line)
{
std::cout << file << ":" << line << ": TEST FAILED: " << msg << std::endl;
exit(1);
}
/**
* Tester is a container for tests.
* Use addTest() to add new test.
* Use runTests() to run all added tests.
*/
class Tester
{
public:
/**
* Construct a Tester.
*
* @param name Name for the Tester instance. Will be printed when running tests.
*/
Tester(const std::string& name)
: name_(name), tests_(


813

被折叠的 条评论
为什么被折叠?



