信号量是c++中实现对有限资源访问控制,现成通过信号量获得对资源访问的许可。可用资源大于0,线程可以对资源进行访问,此时计数器减1。当计数器为0时,不可访问资源,线程进入等待。当资源释放时,线程结束等待,对资源进行访问。信号量限制并发访问的数量,互斥量实现对具体数据的同步。
release函数将技术值加1这是个原子操作,acquire将计数值减1,如果当前为大于1将减1,如果为0,该函数阻塞。信号量常用于生产者和消费者场景以及资源池应用场景。
下面通过一个停车场模拟信号量的使用
#include<array>
#include<vector>
#include<thread>
#include<iostream>
#include<mutex>
#include<algorithm>
#include<sstream>
#include<iomanip>
#include<semaphore>
#include<random>
#include<syncstream>
using namespace std;template<std::size_t N=10>class CarPark
{
public:CarPark() :sem(N) {};int enter() {//代表一辆车进入停车场osyncstream(cout) << "[" << this_thread::get_id() << "]准备进入停车场\n";sem.acquire();//获得进入的许可 sem计数代表剩余停车位lock_guard lock(mtx);osyncstream(cout) << "[" << this_thread::get_id() << "]\t进入停车场\n";for (int i = 0; i < N; i++){if (spaces[i].empty()){stringstream ss;ss<< this_thread::get_id();//相当于车牌号spaces[i] = ss.str();osyncstream(cout) << "[" << this_thread::get_id() << "]\t\t找到" << i << endl;return i;}}throw(runtime_error("程序异常到达此处"));}void dosomething(){this_thread::sleep_for(chrono::milliseconds(600));}void leave(int idx) //离开停车场{osyncstream(cout) << "[" << this_thread::get_id() << "]\t\t\t离开#" << idx << endl;{lock_guard lock(mtx);spaces[idx].clear();//空出的停车位}sem.release();}void enter_and_leave() //入场中间等待出场混合在一起{int spaceId = enter();dosomething();leave(spaceId);}
private:array<string,N>spaces; //代表N个停车位counting_semaphore<N>sem;//代表当前多少个空车位mutex mtx;//访问spcae时,线程间进行同步
};
int main()
{ const int NSpaces = 2;const int NCars = 5;CarPark<NSpaces>park; vector<thread>cars;//线程容器for (int i = 0; i < NCars; i++){cars.emplace_back([&park]() {park.enter_and_leave();});}for (auto& t : cars){t.join();}return 0;
}
运行结果
接下来往一个循环buf读写数据