我在 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 INT\n");
        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
       ^~~~~~~~~~~~~~

我该如何解决这个问题?

尝试使用 emplace_back() 而不是 push_back()

push_back()用的是拷贝构造函数,好像删掉了。

如果您只需要创建一个包含 nw 个元素的向量,您可以执行以下操作:

std::vector<blocking_queue<int>> myVector(nw);

emplace_back 的使用可以工作,但是在执行 emplace_back 时,vector 可能需要调整大小,调整大小可能会触发旧缓冲区和新缓冲区之间的复制。参见 。据我所知,如果你有一个 noexcept move ctor,vector 将使用它而不是复制,但我不知道这是否是实现者可以选择的标准的保证优化去做。无论如何,您的 class 似乎不能移动,因为它的一些成员,即 condition_variablemutex。 因此,在向量中使用这个特定对象可能会出现问题,具体取决于您的用例。

如果您需要能够调整容器的大小,您可以:

  1. 保留vector并使用queue对象的unique_ptr来存储vector内部的元素
  2. 使用 std::list 调整大小时不需要复制对象,因为它是一个链表。

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