要点
- 维护一个资源(在生产者-消费者中即流水线的位置)池,实现put()/get()两个函数。由于对信号量的操作是互斥的,要引入条件变量和信号量。
- 实现资源池类Pool,成员变量:
- mtx : mutex
- cv : condition_variable
- que : queue
- capacity : int
- 实现资源池类Pool,成员函数:
- T get() : 获取一个队列头的资源
- void put(T val) : 放置一个资源到队列末尾
代码实现
#include <iostream>
#include <thread>
#include <mutex>
#include <queue>
using namespace std;
template<typename T>
class Pool {
private:
int capacity;
mutex mtx;
condition_variable cv;
queue<T> que;
public:
Pool(int _capacity) : capacity(_capacity) {}
void put(T val) {
unique_lock<mutex> ulock(mtx);
while (que.size() >= capacity) {
cv.wait(ulock);
}
que.push(val);
cv.notify_all();
cout << "生产了 " << val << endl;
return;
}
T get() {
unique_lock<mutex> ulock(mtx);
while (que.empty()) {
cv.wait(ulock);
}
T ans = que.front();
que.pop();
cv.notify_all();
cout << "消费了 " << ans << endl;
return ans;
}
};
int main() {
cout << "开始程序------" << endl;
Pool<int> pool(3);
thread t1([&pool]() {
for (int i = 0; i < 10; ++i) {
pool.put(i);
this_thread::sleep_for(chrono::milliseconds(100));
}
return;
});
thread t2([&pool]() {
for (int i = 0, ans = 0; i < 10; ++i) {
int res = pool.get();
this_thread::sleep_for(chrono::milliseconds(200));
}
return;
});
t1.join();
t2.join();
return 0;
}
标签:capacity,之手,ulock,生产者,int,que,多线程,cv,pool
From: https://www.cnblogs.com/GaogaoBlog/p/18428065