1

I need to have a std::vector of safequeues of std::pair for a map-reduce problem, but the compiler gives me always the same error because of an ill-formed definition. I've already read a lot of posts but can't still figure out why the code doesn't work. I'm using a safequeue because of multithreading. This is my safe_queue definition:

template <typename T>
class safe_queue
{
private:
  std::mutex              d_mutex;
  std::condition_variable d_condition;
  std::deque<T>           d_queue;
public:

  safe_queue() {}

  void push(T 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 = this->d_queue.back();
    this->d_queue.pop_back();
    return rc;
  }
};

This is the main:

int main(int argc, char * argv[]) {

  std::vector<safe_queue<std::pair<std::string, int>>> task_queues;

  task_queues.push_back(safe_queue<std::pair<std::string, int>>());
  task_queues[0].push(std::pair<std::string, int>("hello", 0));
}

and I'm getting a lot of errors like this:

note: ‘safe_queue<std::pair<std::__cxx11::basic_string<char>, int> >::safe_queue(safe_queue<std::pair<std::__cxx11::basic_string<char>, int> >&&)’ is implicitly deleted because the default definition would be ill-formed:
 class safe_queue

required from here
/usr/include/c++/5/ext/new_allocator.h:120:4: error: use of deleted function ‘safe_queue<std::pair<std::__cxx11::basic_string<char>, int> >::safe_queue(safe_queue<std::pair<std::__cxx11::basic_string<char>, int> >&&)’
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  • 3
    `mutex` does not copy , so it and nothing containing it cannot be `push_back`ed. – user4581301 May 12 '18 at 19:44
  • 1
    Helpful reading: [How should I deal with mutexes in movable types in C++?](https://stackoverflow.com/questions/29986208/how-should-i-deal-with-mutexes-in-movable-types-in-c) – user4581301 May 12 '18 at 19:48
  • About 80% agree with that dupe, but I don't want to hammer this question shut in case someone comes along with a, "Here's how to avoid the problem entirely!" answer. – user4581301 May 12 '18 at 19:54
  • I've read the linked answer and thank you for that, but I'm pretty new to c++ and still can't solve... how do I fix this issue specifically in my code? It's just enough to put `using MutexType = std::mutex; using WriteLock = std::unique_lock; mutable MutexType mut_; ` – Mario Leonardo Salinas May 12 '18 at 20:42
  • The problem you are trying to solve is moderate to advanced. No shame in getting stuck. Everything you put into a `vector` must be copy-able or movable and a `mutex` is neither. The bulk of the proposed duplicate demonstrates how to safely copy and move an object containing a `mutex`. You will need to read and adapt at least the portions on move construction and move assignment from Howard Hinnant's answer to `safe_queue`. – user4581301 May 12 '18 at 21:24

0 Answers0