C++20引入了std::counting_semaphore和std::binary_semaphore,推荐用于线程同步;早期版本可通过POSIX信号量(Linux)或互斥锁与条件变量模拟实现,适用于资源访问控制、生产者-消费者模型等场景。

在C++中,信号量(Semaphore)是一种常用的同步机制,用于控制多个线程对共享资源的访问。C++11标准库本身没有直接提供信号量,但从C++20开始,<semaphore>头文件引入了 std::counting_semaphore 和简化的 std::binary_semaphore。对于早期版本的C++,可以使用POSIX信号量或通过互斥锁和条件变量模拟。
如果你使用的是支持C++20的编译器(如GCC 11+、Clang 14+),可以直接使用标准库提供的信号量类型。
示例代码:创建一个计数信号量,初始值为1(类似二进制信号量):
#include <iostream>
#include <thread>
#include <semaphore>
#include <vector>
std::counting_semaphore<5> sem(1); // 最大允许5个,初始1个
std::vector<int> data;
void worker(int id) {
sem.acquire(); // 等待获取信号量
std::cout << "线程 " << id << " 进入临界区\n";
// 模拟操作共享资源
data.push_back(id);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::cout << "线程 " << id << " 离开临界区\n";
sem.release(); // 释放信号量
}
int main() {
std::vector<std::thread> threads;
for (int i = 1; i <= 3; ++i) {
threads.emplace_back(worker, i);
}
for (auto& t : threads) {
t.join();
}
return 0;
}
关键方法:
立即学习“C++免费学习笔记(深入)”;
在Linux系统中,可以使用POSIX信号量 sem_t,需包含 <semaphore.h>。
#include <iostream>
#include <thread>
#include <semaphore.h>
sem_t posix_sem;
void task(int id) {
sem_wait(&posix_sem); // P操作
std::cout << "线程 " << id << " 正在工作...\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "线程 " << id << " 完成。\n";
sem_post(&posix_sem); // V操作
}
int main() {
sem_init(&posix_sem, 0, 1); // 初始化,非共享,初值1
std::thread t1(task, 1);
std::thread t2(task, 2);
t1.join();
t2.join();
sem_destroy(&posix_sem);
return 0;
}
注意:Windows不原生支持 sem_t,移植性差。
适用于不支持C++20的环境,手动实现一个简单的信号量类。
自定义信号量类:#include <mutex>
#include <condition_variable>
class semaphore {
private:
std::mutex mtx;
std::condition_variable cv;
int count;
public:
semaphore(int c = 0) : count(c) {}
void acquire() {
std::unique_lock<std::mutex> lock(mtx);
while (count == 0) {
cv.wait(lock);
}
--count;
}
void release() {
std::unique_lock<std::mutex> lock(mtx);
++count;
cv.notify_one();
}
};
使用方式与C++20信号量类似,可替换 std::counting_semaphore。
信号量适合以下场景:
例如,在生产者-消费者中,可用两个信号量分别表示空位和数据项数量。
基本上就这些。根据你的编译器版本选择合适的方式。C++20最简洁,旧版本可用模拟实现。以上就是c++++怎么使用信号量进行同步_c++信号量同步方法的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号