我有一个问题,创建了C 中阻塞队列的向量

I have a problem creating a vector of blocking queues in C++

本文关键字:队列 向量 问题 有一个 创建      更新时间:2023-10-16

我正在尝试编写一个简单的程序,该程序可以创建并使用某个数字(变量 - 这是命令行传递的参数(。为了轻松访问它们,我考虑了创建队列的向量。

我正在使用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_variablemutex,您的班级似乎无法移动。因此,在向量内使用此特定对象可能会出现问题,这取决于您的用例。

如果您需要调整容器大小,则可以:

  1. 保留vector并使用queue对象的unique_ptr将元素存储在矢量中
  2. 使用std::list在调整大小时不需要复制对象,因为它是链接的列表。

在这两种情况下,您都可能会失去性能

尝试使用emplace_back()代替push_back()

push_back()使用复制构造函数,似乎已删除。