我正在尝试编写一个简单的程序来创建和使用一定数量(变量 - 它是命令行传递的参数)的阻塞队列。为了轻松访问它们,我考虑创建一个队列 vector 。
我正在使用 g++-8 编译程序。我的队列是由我的教授提供的,所以我无法对其代码进行任何更改。
这是我开发的代码:
阻塞队列.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 INT\n");
return false;
}
int size() {
std::unique_lock<std::mutex> lock(this->d_mutex);
return(d_queue.size());
}
};
#endif // SKYLINE_BLOCKING_QUEUE_HPP
测试.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
元素的 vector ,您可以执行以下操作:
std::vector<blocking_queue<int>> myVector(nw);
emplace_back
的使用可以工作,但是在执行 emplace_back
时,vector
可能需要调整大小,调整大小可能会触发旧缓冲区和旧缓冲区之间的复制新缓冲区。参见 C++ vector emplace_back calls copy constructor .据我所知,如果你有一个 noexcept
move ctor,vector
将使用它而不是复制,但我不知道这是否是标准的保证优化实现者可以选择做的事情。无论如何,您的类似乎不可移动,因为它有一些成员,即 condition_variable
和 mutex
。
因此,在 vector 中使用这个特定对象可能会出现问题,具体取决于您的用例。
如果您需要能够调整容器的大小,您可以:
- 保留
vector
并使用queue
对象的unique_ptr
来存储vector中的元素 - 使用
std::list
,因为它是一个链表,所以在调整大小时不需要复制对象。
在这两种情况下你都可能会失去性能
关于c++ - 我在 C++ 中创建阻塞队列 vector 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56794023/