并发实现对比
libstdc++ 和 libc++ 在并发原语实现上有差异。本文对比两者的实现策略。
一、atomic 对比
1.1 GCC (libstdc++) 的 atomic(源码分析)
libstdc++ atomic:
实现方式:
· 使用 __atomic_* 内建函数
· 支持双字原子优化
· 支持三种锁策略
关键源码:
· atomic_base.h:__atomic_base 类定义
· atomic_lockfree_defines.h:lock-free 属性
性能特点:
· 在支持的平台上 lock-free
· 双字原子操作优化1.2 LLVM (libc++) 的 atomic(源码分析)
libc++ atomic:
实现方式:
· 使用 __atomic_* 内建函数
· 支持 trivial_abi
· 支持 constexpr
关键源码:
· __atomic/atomic.h:atomic 类定义
· __atomic/atomic_sync.h:同步操作
性能特点:
· 与 libstdc++ 类似
· 但更现代的实现二、mutex 对比
mutex 对比:
libstdc++:
· 使用 pthread_mutex
· 支持 timed_mutex
libc++:
· 使用 pthread_mutex
· 支持 timed_mutex三、thread 对比
thread 对比:
libstdc++:
· 使用 pthread_create
· 支持 jthread
libc++:
· 使用 pthread_create
· 支持 jthread四、future/promise 对比
future/promise 对比:
libstdc++:
· shared_state 使用 mutex
· 支持 packaged_task
libc++:
· shared_state 使用 mutex
· 支持 packaged_task延伸阅读
- std::atomic 实现 — atomic 的详细实现
- std::mutex 实现 — mutex 的详细实现
- std::thread 实现 — thread 的详细实现