cpp-pubsub is a fast, lightweight, and thread-safe header-only C++17 library for Publish-Subscribe messaging.
- Header-Only: Simply include
cpppubsub.hppin your project. - Thread-Safe by Design: Uses standard C++ primitives (
<mutex>,<condition_variable>) to guarantee safe multi-threaded dispatching. Multiple publishers and subscribers can operate simultaneously without data races. - Robust Memory Management: Deep integration with C++ smart pointers guarantees memory safety. The broker safely manages
Subscriberlifecycles (no manual unsubscribe needed) and strictly respectsstd::shared_ptrreference counts to prevent memory leaks even when unread messages are dropped from full queues. - Background Workers: Includes a robust
WorkerandSelectorsystem for asynchronous background processing of multiple topics simultaneously. - Type-Safe Topics: Topics and subscribers strictly enforce the type of the messages being passed.
You can easily integrate this into your project via CMake's add_subdirectory or FetchContent. The target cpppubsub::cpppubsub is an INTERFACE library.
include(FetchContent)
FetchContent_Declare(
cpppubsub
GIT_REPOSITORY https://github.com/jonoton/cpp-pubsub.git
GIT_TAG main
)
FetchContent_MakeAvailable(cpppubsub)
target_link_libraries(your_target PRIVATE cpppubsub::cpppubsub)Alternatively, if you cloned the repository locally as a submodule:
add_subdirectory(path/to/cpppubsub)
target_link_libraries(your_target PRIVATE cpppubsub::cpppubsub)#include "cpppubsub.hpp"
#include <iostream>
#include <string>
int main() {
cpppubsub::PubSub broker;
// 1. Subscribe to a topic
auto sub = broker.Subscribe<std::string>("system_events");
// 2. Publish to the topic
broker.Publish("system_events", std::string("Hello World!"));
// 3. Receive the message
if (auto msg = sub->try_receive()) {
std::cout << "Received: " << *msg << std::endl;
}
return 0;
}For full API documentation and advanced usage (like the Worker class), please refer to our GitHub Pages Documentation Site.
Check out the examples/ directory for full compilable examples demonstrating:
- Basic multi-threaded publish/subscribe (
basic_pubsub.cpp) - Worker multiplexing (
worker_pubsub.cpp) - Structured event loops and multi-topic coordination (
advanced_pubsub.cpp)
By default, building the project will compile the examples. You can also build and run the unit tests powered by Google Test.
mkdir build && cd build
cmake .. -DCPPPUBSUB_BUILD_TESTS=ON
cmake --build .
# Run the tests
ctest --output-on-failure
# Run an example
./advanced_pubsub