当前位置:网站首页>LeetCode-1188. Designing finite blocking queues
LeetCode-1188. Designing finite blocking queues
2022-06-29 15:17:00 【Border wanderer】
Implement a thread safe limited blocking queue with the following methods :
BoundedBlockingQueue(int capacity) Constructor initializes the queue , among capacity Represents the maximum queue length .
void enqueue(int element) Add one at the head of the team element. If the queue is full , The calling thread is blocked until the queue is not full .
int dequeue() Returns the end of queue element and removes it from the queue . If the queue is empty , The calling thread is blocked until the queue is not empty .
int size() Returns the number of elements in the current queue .
Your implementation will be accessed simultaneously by multiple threads for testing . Each thread is either a call only enqueue Method's producer thread , Or one that just calls dequeue Method .size Method will be called after each test case .
Please do not use the built-in limited blocking queue implementation , Otherwise, the interview will not pass .
Example 1:
Input :
1
1
["BoundedBlockingQueue","enqueue","dequeue","dequeue","enqueue","enqueue","enqueue","enqueue","dequeue"]
[[2],[1],[],[],[0],[2],[3],[4],[]]
Output :
[1,0,2,2]
explain :
Number of producer threads = 1
Number of consumer threads = 1
BoundedBlockingQueue queue = new BoundedBlockingQueue(2); // Use capacity = 2 Initialize queue .
queue.enqueue(1); // The producer thread will 1 Insert queue .
queue.dequeue(); // Consumer thread call dequeue And back to 1 .
queue.dequeue(); // Because the queue is empty , Consumer thread blocked .
queue.enqueue(0); // The producer thread will 0 Insert queue . The consumer thread is unblocked and 0 Pop up the queue and return .
queue.enqueue(2); // The producer thread will 2 Insert queue .
queue.enqueue(3); // The producer thread will 3 Insert queue .
queue.enqueue(4); // The producer thread has reached the maximum queue length 2 And blocked .
queue.dequeue(); // The consumer thread will 2 Eject from the queue and return . The producer thread unblocks and 4 Insert queue .
queue.size(); // There are also 2 Elements .size() Method is invoked at the end of each test case .
Example 2:
Input :
3
4
["BoundedBlockingQueue","enqueue","enqueue","enqueue","dequeue","dequeue","dequeue","enqueue"]
[[3],[1],[0],[2],[],[],[],[3]]
Output :
[1,0,2,1]
explain :
Number of producer threads = 3
Number of consumer threads = 4
BoundedBlockingQueue queue = new BoundedBlockingQueue(3); // Use capacity = 3 Initialize queue .
queue.enqueue(1); // Producer thread P1 take 1 Insert queue .
queue.enqueue(0); // Producer thread P2 take 0 Insert queue .
queue.enqueue(2); // Producer thread P3 take 2 Insert queue .
queue.dequeue(); // Consumer thread C1 call dequeue.
queue.dequeue(); // Consumer thread C2 call dequeue.
queue.dequeue(); // Consumer thread C3 call dequeue.
queue.enqueue(3); // One of the producer threads will 3 Insert queue .
queue.size(); // There are also 1 Elements .
Because the producer / The number of consumer threads may be greater than 1 , We don't know how threads are scheduled by the operating system , Even if the input seems to imply order . So any output [1,0,2] or [1,2,0] or [0,1,2] or [0,2,1] or [2,0,1] or [2,1,0] Are acceptable .
Tips :
1 <= Number of Prdoucers <= 8
1 <= Number of Consumers <= 8
1 <= size <= 30
0 <= element <= 20
enqueue Number of calls Greater than or equal to dequeue Number of calls .
enque, deque and size At most called 40 Time
#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);
/* The queue is full , wait for */
cv.wait(uq, [=]() {
return (m_q.size() < cap);
});
m_q.push(element);
cv.notify_one();
}
int dequeue() {
std::unique_lock<mutex> uq(mtx);
/* The queue is empty , wait for */
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;
};边栏推荐
- 明德扬XILINX-K7-325T/410T核心板数据手册
- What should phpcms do when it sends an upgrade request to the official website when it opens the background home page?
- go学习(四、面向接口)
- 极化SAR几种成像模式
- Lumiprobe 点击化学丨非荧光叠氮化物:叠氮化物-PEG3-OH
- Unity C basic review 29 - Generic delegation (p451)
- Is it reliable to invest in REITs funds? Is REITs funds safe
- Render follows, encapsulating a form and adding data to the table
- 我 35 岁,可以转行当程序员吗?
- MySQL定时整库备份&滚动删除指定日期前的备份数据
猜你喜欢

PostgreSQL learning (based on rookie course)

ROS notes (10) - Launch file startup

Pytorch two-dimensional multi-channel convolution operation method

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear

Basic use of text preprocessing library Spacy (quick start)

Lumiprobe 活性染料丨羧酸:Sulfo-Cyanine7.5羧酸

明德扬XILINX-K7-325T/410T核心板数据手册

SOFARegistry 源码|数据同步模块解析

信息学奥赛一本通1194:移动路线

Unity C # basic review 26 - first acquaintance Commission (p447)
随机推荐
极化SAR地表分类
Informatics Olympiad 1001:hello, world!
MCS:离散随机变量——几何分布
Sofaregistry source code | data synchronization module analysis
卫星运动的微分方程
MCS: discrete random variables - geometric distribution
Uncover the practice of Baidu intelligent test in the field of automatic test execution
Northwestern Polytechnic University attacked by overseas e-mail
message from server: “Host ‘xxxxxx‘ is blocked because of many connection errors; unblock with ‘m
Lumiprobe 活性染料丨杂染料:BDP FL 神经酰胺
真正的软件测试人员 =“半个产品+半个开发”?
微信公众号—菜单
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
如临现场的视觉感染力,NBA决赛直播还能这样看?
Biovendor free light chain( κ and λ) Chemical properties of ELISA Kit
Lumiprobe 活性染料丨羧酸:Sulfo-Cyanine7.5羧酸
Implementing redis distributed locks using custom annotations
Unity C basic review 28 - delegation with return (p449)
Lumiprobe reactive dye carboxylic acid: sulfo cyanine7.5 carboxylic acid
Konva series Tutorial 4: drawing attributes