mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 17:26:48 +08:00
Root cause: FreeBSD's robust mutex implementation (libthr) maintains a per-thread robust list of mutexes. The error 'rb error 14' (EFAULT - Bad address) indicates that the robust list contained a dangling pointer to a destroyed mutex. When a mutex object is destroyed (via close() or clear()), if the mutex is still in the current thread's robust list, FreeBSD's libthr may try to access it later and encounter an invalid pointer, causing a segmentation fault. This happened in MutexTest.TryLockExceptionSafety because: 1. The test called try_lock() which successfully acquired the lock 2. The test ended without calling unlock() 3. The mutex destructor called close() 4. close() called pthread_mutex_destroy() on a mutex that was: - Still locked by the current thread, OR - Still in the thread's robust list Solution: Call pthread_mutex_unlock() before pthread_mutex_destroy() in both close() and clear() methods. This ensures: 1. The mutex is unlocked if we hold the lock 2. The mutex is removed from the thread's robust list 3. Subsequent pthread_mutex_destroy() is safe We ignore errors from pthread_mutex_unlock() because: - If we don't hold the lock, EPERM is expected and harmless - If the mutex is already unlocked, this is a no-op - Even if there's an error, we still want to proceed with cleanup This fix is platform-agnostic and should not affect Linux/QNX behavior, as both also use pthread robust mutexes with similar semantics. Fixes the segfault in MutexTest.TryLockExceptionSafety on FreeBSD 15. |
||
|---|---|---|
| .github/workflows | ||
| 3rdparty | ||
| demo | ||
| include/libipc | ||
| src | ||
| test | ||
| .gitignore | ||
| CMakeLists.txt | ||
| LICENSE | ||
| performance.xlsx | ||
| README.md | ||
cpp-ipc (libipc) - C++ IPC Library
A high-performance inter-process communication library using shared memory on Linux/Windows/FreeBSD.
- Compilers with C++17 support are recommended (msvc-2017/gcc-7/clang-4)
- No other dependencies except STL.
- Only lock-free or lightweight spin-lock is used.
- Circular array is used as the underline data structure.
ipc::routesupports single write and multiple read.ipc::channelsupports multiple read and write. (Note: currently, a channel supports up to 32 receivers, but there is no such a limit for the sender.)- Broadcasting is used by default, but user can choose any read/ write combinations.
- No long time blind wait. (Semaphore will be used after a certain number of retries.)
- Vcpkg way of installation is supported. E.g.
vcpkg install cpp-ipc
Usage
See: Wiki
Performance
| Environment | Value |
|---|---|
| Device | Lenovo ThinkPad T450 |
| CPU | Intel® Core™ i5-4300U @ 2.5 GHz |
| RAM | 16 GB |
| OS | Windows 7 Ultimate x64 |
| Compiler | MSVC 2017 15.9.4 |
Unit & benchmark tests: test
Performance data: performance.xlsx
Reference
- Lock-Free Data Structures | Dr Dobb's
- Yet another implementation of a lock-free circular array queue | CodeProject
- Lock-Free 编程 | 匠心十年 - 博客园
- 无锁队列的实现 | 酷 壳 - CoolShell
- Implementing Condition Variables with Semaphores
使用共享内存的跨平台(Linux/Windows/FreeBSD,x86/x64/ARM)高性能IPC通讯库
- 推荐支持C++17的编译器(msvc-2017/gcc-7/clang-4)
- 除STL外,无其他依赖
- 无锁(lock-free)或轻量级spin-lock
- 底层数据结构为循环数组(circular array)
ipc::route支持单写多读,ipc::channel支持多写多读【注意:目前同一条通道最多支持32个receiver,sender无限制】- 默认采用广播模式收发数据,支持用户任意选择读写方案
- 不会长时间忙等(重试一定次数后会使用信号量进行等待),支持超时
- 支持Vcpkg方式安装,如
vcpkg install cpp-ipc
使用方法
详见:Wiki
性能
| 环境 | 值 |
|---|---|
| 设备 | 联想 ThinkPad T450 |
| CPU | 英特尔® Core™ i5-4300U @ 2.5 GHz |
| 内存 | 16 GB |
| 操作系统 | Windows 7 Ultimate x64 |
| 编译器 | MSVC 2017 15.9.4 |
单元测试和Benchmark测试: test
性能数据: performance.xlsx