# mccc-message-cpp **Repository Path**: liudegui/mccc-message-cpp ## Basic Information - **Project Name**: mccc-message-cpp - **Description**: 基于 MCCC 无锁消息总线的 SendMessage/PostMessage 线程间消息传递模式 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-16 - **Last Updated**: 2026-02-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: Cpp, 消息传递, MCCC, 多线程, SendMessage ## README # mccc-message-cpp SendMessage/PostMessage inter-thread messaging pattern built on [MCCC](https://gitee.com/liudegui/mccc-bus) lock-free message bus. ## Overview This project demonstrates how to replace traditional `mutex + priority_queue + promise/future` inter-thread messaging with MCCC's lock-free MPSC ring buffer. It provides a thin `MessageService` wrapper that maps Windows-style `SendMessage` (synchronous) and `PostMessage` (asynchronous) to MCCC primitives. ## Key Features | Feature | Traditional (mutex) | MCCC-based | |---------|-------------------|------------| | Thread safety | `std::mutex` + `std::condition_variable` | Lock-free CAS (MPSC) | | Memory allocation | `std::shared_ptr>` per sync msg | Pre-allocated `ResponseSlot` pool | | Priority | `std::priority_queue` (O(log n) insert) | Admission control (O(1), 3 levels) | | Type safety | `int msg_id` + `int w_param` | `std::variant` | | Backpressure | None (unbounded queue growth) | Priority-based admission thresholds | ## Architecture ``` Producer Threads Consumer Thread (SendMessage/PostMessage) (ProcessBatch) | | | lock-free CAS enqueue | batch dequeue v v +------------------------------------------+ | MCCC AsyncBus (MPSC Ring Buffer) | | Priority Admission: HIGH > MEDIUM > LOW | +------------------------------------------+ | +---------+---------+ | | AsyncMessage SyncRequest (fire & forget) (response slot) | | handler(...) handler(...) -> result | ResponseSlot.SetResult() | Producer.WaitResult() ``` ## Build ```bash mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Debug make -j$(nproc) ctest --output-on-failure ``` ### CMake Options | Option | Default | Description | |--------|---------|-------------| | `MCCC_MESSAGE_BUILD_EXAMPLES` | ON | Build example programs | | `MCCC_MESSAGE_BUILD_TESTS` | ON | Build Catch2 tests | ## Examples - **basic_demo** -- Mirrors the original CSDN article's `main()` function - **priority_demo** -- MCCC priority admission control (HIGH/MEDIUM/LOW) - **sync_async_demo** -- Multi-producer concurrent SendMessage/PostMessage ## Tests 8 test cases covering: - `ResponsePool` lock-free acquire/release - `PostMessage` async delivery - `SendMessage` sync request-reply - Multi-producer concurrent mixed sync/async - Service lifecycle management All tests pass under ASan + UBSan. ## Dependencies - C++17 compiler (GCC 7+ / Clang 5+) - [MCCC](https://gitee.com/liudegui/mccc-bus) (auto-fetched via CMake FetchContent) - [Catch2 v3](https://github.com/catchorg/Catch2) (auto-fetched for tests) ## License MIT