std :: mutex как член класса, и хранить класс obect для контейнера - PullRequest
2 голосов
/ 05 марта 2019

Ниже приведен минимальный код для воспроизведения ошибки.

#include <iostream>
#include <mutex>
#include <vector>

class A {
    std::mutex mutex;
    public:
    A(){};
};
int main() 
{
    std::vector<std::pair<std::string,A>> aa;
    A a;
    //aa.push_back(std::make_pair(std::string("aa"),A()));
    //aa.push_back(std::make_pair(std::string("aa"),a));
    aa.push_back(std::make_pair(std::string("aa"),std::move(a)));    
}

Ниже приведена ошибка.

Microsoft (R) C / C ++ Оптимизирующая версия компилятора 19.16.27026.1 для x64 Авторское право (C) Microsoft Corporation. Все права защищены.

>   C:\Program Files (x86)\Microsoft Visual
> Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocale(319):
> warning C4530: C++ exception handler used, but unwind semantics are
> not enabled. Specify /EHsc    C:\Program Files (x86)\Microsoft Visual
> Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\utility(405):
> error C2440: '<function-style-cast>': cannot convert from 'initializer
> list' to '_Mypair'    C:\Program Files (x86)\Microsoft Visual
> Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\utility(405):
> note: No constructor could take the source type, or constructor
> overload resolution was ambiguous
>   ..\examples\json_object\json.cpp(16): note: see reference to function
> template instantiation 'std::pair<std::string,A>
> std::make_pair<std::string,A>(_Ty1 &&,_Ty2 &&)' being compiled            with
>           [
>               _Ty1=std::string,
>               _Ty2=A          ]

Аналогичная ошибка для gcc компилятора. Когда я удаляю std::mutex из класса ИЛИ не помещаю объект в std::vector, тогда он прекрасно компилируется.

Ответы [ 3 ]

7 голосов
/ 05 марта 2019

Согласно документации на std::mutex.

std::mutex не является ни копируемым , ни подвижным .

Поскольку класс A содержит переменную std::mutex mutex, он также не является подвижным.

1 голос
/ 07 марта 2019

Как указывал PW, std::mutex не является ни копируемым, ни подвижным, и на то есть веские причины.Весь смысл наличия мьютекса заключается в защите от одновременного многопоточного доступа к некоторым данным.Сама операция перемещения должна быть защищена, и мьютекс должен быть использован для операции перемещения.

В следующем примере класс получает некоторые подвижные данные и показывает, какмьютекс должен использоваться в операции перемещения (операции копирования будут аналогичными):

#include <iostream>
#include <mutex>
#include <vector>
#include <memory>

class A {
public:
  A() {};

  // Move constructor
  A(A&& other) {
    std::lock_guard<std::mutex> guard(other.m_mutex);
    m_data = std::move(other.m_data);
  }

  // Move operator
  A& operator=(A&& other) {
    if (this == &other) return *this;

    // Lock this and other in a consistent order to prevent deadlock
    std::mutex* first;
    std::mutex* second;
    if (this < &other) {
      first = &this->m_mutex;
      second = &other.m_mutex;
    } else {
      first = &other.m_mutex;
      second = &this->m_mutex;
    }
    std::lock_guard<std::mutex> guard1(*first);
    std::lock_guard<std::mutex> guard2(*second);

    // Now both this and other are safe to access.  Do the actual data move.
    m_data = std::move(other.m_data);
    return *this;
  }

private:
  std::mutex m_mutex;
  std::unique_ptr<int> m_data;
};

int main() {
  std::vector<std::pair<std::string,A>> aa;
  A a1;
  A a2;
  a1 = std::move(a2);
  aa.emplace_back("aa", std::move(a1));
}
0 голосов
/ 05 марта 2019

Как указал P.W, и намек, предоставленный причудливым человеком, я придумаю ниже решение.

#include <iostream>
#include <mutex>
#include <vector>
#include <memory>

class A {
    std::mutex mutex;
    public:
    A(){};
};
int main() 
{
    std::vector<std::pair<std::string,std::shared_ptr<A>>> aa;
    A a;
    //aa.push_back(std::make_pair(std::string("aa"),A()));
    //aa.push_back(std::make_pair(std::string("aa"),a));
    aa.push_back(std::make_pair(std::string("aa"),std::make_shared<A>()));   
}

Я изменил свой контейнер для хранения умного указателя объекта вместо самого объекта.

...