当前位置:网站首页>LeetCode-1188. 设计有限阻塞队列
LeetCode-1188. 设计有限阻塞队列
2022-06-29 14:49:00 【边界流浪者】
实现一个拥有如下方法的线程安全有限阻塞队列:
BoundedBlockingQueue(int capacity) 构造方法初始化队列,其中capacity代表队列长度上限。
void enqueue(int element) 在队首增加一个element. 如果队列满,调用线程被阻塞直到队列非满。
int dequeue() 返回队尾元素并从队列中将其删除. 如果队列为空,调用线程被阻塞直到队列非空。
int size() 返回当前队列元素个数。
你的实现将会被多线程同时访问进行测试。每一个线程要么是一个只调用enqueue方法的生产者线程,要么是一个只调用dequeue方法的消费者线程。size方法将会在每一个测试用例之后进行调用。
请不要使用内置的有限阻塞队列实现,否则面试将不会通过。
示例 1:
输入:
1
1
["BoundedBlockingQueue","enqueue","dequeue","dequeue","enqueue","enqueue","enqueue","enqueue","dequeue"]
[[2],[1],[],[],[0],[2],[3],[4],[]]
输出:
[1,0,2,2]
解释:
生产者线程数目 = 1
消费者线程数目 = 1
BoundedBlockingQueue queue = new BoundedBlockingQueue(2); // 使用capacity = 2初始化队列。
queue.enqueue(1); // 生产者线程将 1 插入队列。
queue.dequeue(); // 消费者线程调用 dequeue 并返回 1 。
queue.dequeue(); // 由于队列为空,消费者线程被阻塞。
queue.enqueue(0); // 生产者线程将 0 插入队列。消费者线程被解除阻塞同时将 0 弹出队列并返回。
queue.enqueue(2); // 生产者线程将 2 插入队列。
queue.enqueue(3); // 生产者线程将 3 插入队列。
queue.enqueue(4); // 生产者线程由于队列长度已达到上限 2 而被阻塞。
queue.dequeue(); // 消费者线程将 2 从队列弹出并返回。生产者线程解除阻塞同时将4插入队列。
queue.size(); // 队列中还有 2 个元素。size()方法在每组测试用例最后调用。
示例 2:
输入:
3
4
["BoundedBlockingQueue","enqueue","enqueue","enqueue","dequeue","dequeue","dequeue","enqueue"]
[[3],[1],[0],[2],[],[],[],[3]]
输出:
[1,0,2,1]
解释:
生产者线程数目 = 3
消费者线程数目 = 4
BoundedBlockingQueue queue = new BoundedBlockingQueue(3); // 使用capacity = 3初始化队列。
queue.enqueue(1); // 生产者线程 P1 将 1 插入队列。
queue.enqueue(0); // 生产者线程 P2 将 0 插入队列。
queue.enqueue(2); // 生产者线程 P3 将2插入队列。
queue.dequeue(); // 消费者线程 C1 调用 dequeue。
queue.dequeue(); // 消费者线程 C2 调用 dequeue。
queue.dequeue(); // 消费者线程 C3 调用 dequeue。
queue.enqueue(3); // 其中一个生产者线程将3插入队列。
queue.size(); // 队列中还有 1 个元素。
由于生产者/消费者线程的数目可能大于 1 ,我们并不知道线程如何被操作系统调度,即使输入看上去隐含了顺序。因此任意一种输出[1,0,2]或[1,2,0]或[0,1,2]或[0,2,1]或[2,0,1]或[2,1,0]都可被接受。
提示:
1 <= Number of Prdoucers <= 8
1 <= Number of Consumers <= 8
1 <= size <= 30
0 <= element <= 20
enqueue的调用次数 大于等于 dequeue 的调用次数。
enque, deque 和 size 最多被调用 40 次
#include <iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
using namespace std;
class BoundedBlockingQueue {
public:
BoundedBlockingQueue(int capacity) {
cap = capacity;
}
void enqueue(int element) {
std::unique_lock<mutex> uq(mtx);
/* 队列已满,等待 */
cv.wait(uq, [=]() {
return (m_q.size() < cap);
});
m_q.push(element);
cv.notify_one();
}
int dequeue() {
std::unique_lock<mutex> uq(mtx);
/* 队列为空,等待 */
cv.wait(uq, [=]() {
return (!m_q.empty());
});
int ret = m_q.front();
m_q.pop();
cv.notify_one();
return ret;
}
int size() {
std::unique_lock<mutex> uq(mtx);
return m_q.size();
}
private:
int cap;
condition_variable cv;
mutex mtx;
queue<int> m_q;
};边栏推荐
- Real software testers = "half product + Half development"?
- 中国软冰淇淋市场预测与投资前景研究报告(2022版)
- Is it reliable to invest in REITs funds? Is REITs funds safe
- Unity C basic review 29 - Generic delegation (p451)
- I log in to the RDB database and prompt that the master account authorization is required. How do I know who to call?
- Bash summary online log
- symfony框架安全组件(security)防火墙配置
- ROS 笔记(10)— launch 文件启动
- Uncover the practice of Baidu intelligent test in the field of automatic test execution
- 信息学奥赛一本通1194:移动路线
猜你喜欢
随机推荐
信息学奥赛一本通1003:对齐输出
Configuration tutorial for swagger2
Can futures accounts be opened offline? Is it safe to open an account online?
【牛客网刷题系列 之 Verilog快速入门】~ 移位运算与乘法
If I am in Foshan, where can I open an account? Is it safe to open an account online?
信息学奥赛一本通1000:入门测试题目
Informatics Olympiad all in one 2062: movie tickets
I want to search the hundreds of nodes in the data warehouse. Can I check a table used in the SQL
BioVendor游离轻链(κ和λ)Elisa 试剂盒的化学性质
二级指针
打造一个 API 快速开发平台,牛逼!
kubernetes Unable to connect to the server: x509: certificate has expired or is not yet valid
模电 2个NPN管组成的恒流源电路分析
Hi, you have a code review strategy to check
Lumiprobe 活性染料丨氨基染料:花青5胺
Huashu high tech rushes to the scientific innovation board: the actual controller xuxiaoshu and his son, who plan to raise 660million yuan, are both American nationals
Lumiprobe reactive dye cycloalkyne dye: af488 dbco, 5 isomer
Unity C# 基础复习29——泛型委托(P451)
Netease strict selection offline data warehouse quality construction practice
Lumiprobe deoxyribonucleic acid alkyne DT phosphimide









