简体   繁体   中英

Mutex as object attribute and vector of objects

I'm working on a bookstore management project with multiple threads. I have a class, Shelf() that contains a mutex as attribute. When I compile I get the following error:

error: use of deleted function 'Shelf& Shelf::operator=(const Shelf&)'
    *__result = *__first;

note: 'Shelf& Shelf::operator=(const Shelf&)' is implicitly declared as deleted because 'Shelf' declares a move constructor or move assignment operator
class Shelf {

My project structure is as follows:
1. Book() has some strings like : name, genre...
2. Shelf() has : mutable mutex and an unordered_map of id's and Book*
3. Library() has : vector of Shelf objects.
I saw here How should I deal with mutexes in movable types in C++? that mutexes are not copy/moveable so I followed the instructions of @HowardHinnant answer.

typedef std::unordered_map<Id_t, Book *> hash_map_books_t;
class Shelf {

    using MutexType = std::mutex;
    using ReadLock = std::unique_lock<MutexType>;
    using WriteLock = std::unique_lock<MutexType>;

private:
    //ATTRIBUTES

    mutable MutexType m_mutex;
    std::string m_genre;
    hash_map_books_t m_shelf;

public:

    //CONSTRUCTORS & MOVE & COPY & DESTRUCTORS

    Shelf() = default;

    ~Shelf(){
        for (auto b : m_shelf) {
            delete b.second;
        }
        m_shelf.clear();
    }

    Shelf(Shelf &&shelf) noexcept{

        WriteLock rhs_lk(shelf.m_mutex);

        m_genre = std::move(shelf.m_genre);
        m_shelf = std::move(shelf.m_shelf);

    }

    Shelf(const Shelf &a){

        ReadLock rhs_lk(a.m_mutex);
        m_genre = a.m_genre;
        m_shelf = a.m_shelf;
    }
    Shelf& operator=(Shelf &&a) noexcept{

        if (this != &a) {
            WriteLock lhs_lk(m_mutex, std::defer_lock);
            WriteLock rhs_lk(a.m_mutex, std::defer_lock);

            std::lock(lhs_lk, rhs_lk);

            m_genre = std::move(a.m_genre);
            m_shelf = std::move(a.m_shelf);
        }
        return *this;
    }
};

And even if is not the purpose of my question I'm open for other structures that you could tell me.

As the error message explains, you need to provide a copy-assignment operator, eg:

Shelf& operator= (const Shelf &a)
{
    if (this != &a)
    {
        WriteLock lhs_lk (m_mutex, std::defer_lock);
        ReadLock rhs_lk (a.m_mutex, std::defer_lock);
        std::lock (lhs_lk, rhs_lk);

        m_genre = a.m_genre;
        m_shelf = a.m_shelf;
    }
    return *this;
}

The presence of either a user-defined move constructor or a user-defined move assignment operator makes this necessary. You have both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM