Volatile keyword and thread safety

I have an abstract base class

class Map {
  public:
    virtual Value get(Key const &) const;
};

database class from an external library

class Database {
  public:
    // logically const and thread-safe
    Value get(Key const &key);
};

and I started with implementation for example

class PersistentMap : public Map {
    Database db;

  public:
    Value get(Key const &key) const
    { return const_cast<Database &>(db).get(key); }
};

As the number const_castgrew beyond, I got rid of them by adding a qualifier mutableto PersistentMap::db(and a comment to remind myself of the ugliness).

  • Was my first attempt const_castthread safe?
  • Is my new approach thread safe or should it dbalso be noted volatile?
+3
source share
1 answer

, Database:: get . , , , const_cast mutable. volatile .

+1

Source: https://habr.com/ru/post/1786766/


All Articles