在面试中遇到了“手写阻塞队列”问题,事后进行了完善,代码如下:
测试代码
// 调用示例 // 阻塞队列 BlockQueue<int> blockqueue(5); // 入队列操作 std::thread producer([&]() { for (int i = 0; i < 8; ++i) { std::cout << "push value:" << i << std::endl; blockqueue.push(i); } }); // 出队列操作 std::thread consumer([&](){ for (int i = 0; i < 8; ++i) { int value = blockqueue.pop(); std::cout << "pop value:" << value << std::endl; } }); producer.join(); consumer.join();源码
#ifndef BlockingQueue_h_ #define BlockingQueue_