Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 1.08 KB

README.md

File metadata and controls

56 lines (41 loc) · 1.08 KB

MessageQueue

A fast and lightweight message queue for inter-thread communication in C++11, with easy-to-use support for timeout events.

Example

See example.cpp. You can compile and run the example by the following commands.

cd <project directory>
make
./example

and the output will be

test
hello
end

How to use this

This is a header-only library. You can just copy messagequeue.hpp to your project and #include it in your source files.

To create a message queue:

messagequeue<Type> q;

To implement an event loop:

Type msg;
while(1) {
    msg = q.pop();     // will block until one is available
    process_message(msg);
}

To send a message to a queue: (which is probably used in another worker thread, and that thread will be unblocked)

q.push(msg);

To set a timeout message that will be automatically pushed into the queue after 500 milliseconds:

auto timer = q.setTimeout(msg, 500);

// probably cancel the timer before it alarms
q.clearTimeout(timer);

License

MIT License