c++17及更早版本无原生信号量,需用std::condition_variable+std::mutex+计数器手动实现;c++20引入std::counting_semaphore,但依赖编译器和系统支持,手写轻量semaphore类更稳妥,关键为m_count、m_mutex、m_cv三要素。

为什么标准 C++ 没有 std::semaphore(直到 C++20)
在 C++17 及更早版本中,std::mutex、std::condition_variable 等是同步主力,但没有原生信号量。很多开发者误以为缺了它就写不了资源计数控制——其实可以用 std::condition_variable + std::mutex + 计数器手动模拟,而且逻辑清晰、可控性强。
C++20 引入了 std::counting_semaphore(注意不是 std::semaphore),但它要求编译器支持(如 GCC 11+、Clang 12+、MSVC 19.29+),且底层依赖系统信号量或 futex,跨平台行为未必一致。如果你的目标环境不明确支持 C++20,手写一个轻量 Semaphore 类反而更稳妥。
手写 Semaphore 类的关键成员和线程安全设计
核心是三个东西:一个整型计数器(m_count)、一把互斥锁(m_mutex)、一个条件变量(m_cv)。acquire() 阻塞直到计数 > 0;release() 增加计数并唤醒等待者。
常见错误包括:未用 while 循环检查条件(虚假唤醒)、release() 不加锁(导致 m_count 竞态)、忘记在 acquire() 中用 wait() 的 predicate 版本。
立即学习“C++免费学习笔记(深入)”;
-
acquire()必须用m_cv.wait(m_mutex, [&]{ return m_count > 0; });,不能用if (m_count -
release()要先加锁、改计数、再notify_one()或notify_all();若只唤醒一个,多个等待线程可能仍阻塞 - 构造时传入初始值,应校验非负:若为负,抛
std::invalid_argument
C++11 兼容的手写 Semaphore 示例
以下实现无外部依赖,可直接编译运行(C++11 起):
#include <mutex>
#include <condition_variable>
class Semaphore {
std::mutex m_mutex;
std::condition_variable m_cv;
int m_count;
public:
explicit Semaphore(int count = 0) : m_count(count) {
if (count < 0) throw std::invalid_argument("Semaphore count cannot be negative");
}
void acquire() {
std::unique_lock<std::mutex> lock(m_mutex);
m_cv.wait(lock, [&] { return m_count > 0; });
--m_count;
}
void release() {
std::unique_lock<std::mutex> lock(m_mutex);
++m_count;
m_cv.notify_one(); // 或 notify_all(),依场景选
}
};
使用示例:限制最多 2 个线程同时访问某资源:
#include <thread>
#include <iostream>
Semaphore sem(2);
void worker(int id) {
sem.acquire();
std::cout << "Worker " << id << " entered\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Worker " << id << " leaving\n";
sem.release();
}
int main() {
std::thread t1(worker, 1);
std::thread t2(worker, 2);
std::thread t3(worker, 3);
std::thread t4(worker, 4);
t1.join(); t2.join(); t3.join(); t4.join();
}
用 std::counting_semaphore(C++20)要注意什么
虽然标准提供了,但实际使用时容易忽略两点:
- 它不提供
try_acquire()的超时版本(C++20 只有try_acquire()无参版),需要配合std::chrono手动轮询,性能差 - 某些平台(如 Windows MinGW)可能未完全实现,
std::counting_semaphore行为可能退化为互斥锁,失去计数语义 - 初始化值必须是常量表达式(
constexpr),不能是运行时变量,否则编译失败
所以即使你用 C++20,对简单场景(如限流、池管理),手写 Semaphore 依然更透明、易调试、兼容性更好。
真正复杂的是需要跨进程、带优先级唤醒、或与操作系统信号量深度绑定的场景——那得用 POSIX sem_t 或 Windows CreateSemaphore,而不是标准库。











