我有一个问题,创建了C 中阻塞队列的向量
I have a problem creating a vector of blocking queues in C++
我正在尝试编写一个简单的程序,该程序可以创建并使用某个数字(变量 - 这是命令行传递的参数(。为了轻松访问它们,我考虑了创建队列的向量。
我正在使用G -8来编译程序。我的队列是由我的教授提供的,因此我无法对其代码进行任何更改。
这是我开发的代码:
blocking_queue.hpp
#ifndef SKYLINE_BLOCKING_QUEUE_HPP
#define SKYLINE_BLOCKING_QUEUE_HPP
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <deque>
#include <vector>
#include <chrono>
#include <cstddef>
#include <math.h>
#include <string>
#include <thread>
using namespace std::literals::chrono_literals;
//
// needed a blocking queue
// here is a sample queue.
//
template <typename T>
class blocking_queue
{
private:
std::mutex d_mutex;
std::condition_variable d_condition;
std::deque<T> d_queue;
public:
blocking_queue(){}
void push(T const& value) {
{
std::unique_lock<std::mutex> lock(this->d_mutex);
d_queue.push_front(value);
}
this->d_condition.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=]{return !this->d_queue.empty(); });
T rc(std::move(this->d_queue.back()));
this->d_queue.pop_back();
return rc;
}
bool is_empty() {
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=]{return !this->d_queue.empty(); });
printf("ADDED A INTn");
return false;
}
int size() {
std::unique_lock<std::mutex> lock(this->d_mutex);
return(d_queue.size());
}
};
#endif // SKYLINE_BLOCKING_QUEUE_HPP
test.cpp
int main(char argc, char* argv[]) {
nw = atoi(argv[0]);
vector<blocking_queue<int>> myVector;
for(int i = 0; i < nw; i++) {
myVector.emplace_back();
}
}
当我尝试编译程序时,G 给我以下错误:
error: use of deleted function ‘blocking_queue<int>::blocking_queue(blocking_queue<int>&&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ./test.cpp:10:
./blocking_queue.hpp:28:7: note: ‘blocking_queue<int>::blocking_queue(blocking_queue<int>&&)’ is implicitly deleted because the default definition would be ill-formed:
class blocking_queue
^~~~~~~~~~~~~~
./blocking_queue.hpp:28:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from /usr/include/c++/8/mutex:43,
from ./blocking_queue.hpp:9,
from ./test.cpp:10:
/usr/include/c++/8/bits/std_mutex.h:97:5: note: declared here
mutex(const mutex&) = delete;
^~~~~
In file included from ./test.cpp:10:
./blocking_queue.hpp:28:7: error: use of deleted function ‘std::condition_variable::condition_variable(const std::condition_variable&)’
class blocking_queue
^~~~~~~~~~~~~~
如何解决问题?
如果您只需要创建nw
元素的向量,则可以执行以下操作:
std::vector<blocking_queue<int>> myVector(nw);
使用emplace_back
可以使用,但是在执行emplace_back
时,vector
可能需要调整大小和调整大小可能会触发旧缓冲区和新缓冲区之间的副本。请参阅C vector emplace_back调用复制构造函数。据我所知,如果您有noexcept
MOVE CTOR,vector
将使用它而不是复制,但我不知道这是否是从实施者可以选择做的事情的标准中保证的优化。无论如何,由于其某些成员,即condition_variable
和mutex
,您的班级似乎无法移动。因此,在向量内使用此特定对象可能会出现问题,这取决于您的用例。
如果您需要调整容器大小,则可以:
- 保留
vector
并使用queue
对象的unique_ptr
将元素存储在矢量中 - 使用
std::list
在调整大小时不需要复制对象,因为它是链接的列表。
在这两种情况下,您都可能会失去性能
尝试使用emplace_back()
代替push_back()
。
push_back()
使用复制构造函数,似乎已删除。
相关文章:
- 向量成员在管理类指针C++队列时丢失
- C++ Poco - 如何创建通知队列的向量?
- 为什么我们需要在优先级队列声明中添加一个向量作为参数?
- C++:自定义数据类型向量错误的队列
- C++ 如何将unique_ptr队列添加到向量
- 为什么优先级队列是使用堆实现的,而我们可以更有效地使用向量来实现它
- 我有一个问题,创建了C 中阻塞队列的向量
- 在C++中使用队列和堆栈对 2 个向量进行加减
- 如何以相反的顺序将整个向量复制到队列中
- 如何获得向量队列的前部或顶部元素
- 使用优先级队列初始化向量
- 在队列中插入向量元素
- 使用更大值和向量声明优先级队列
- 如何构造队列向量
- 创建固定大小队列的向量(提升循环队列)
- 将向量、队列和指向函数的指针传递给函数
- 从队列中弹出向量
- 向量和队列C++
- 用户定义的"队列"类的向量
- 使用向量C++的双精度的优先级队列