I want to execute a read-only method on an object marked as const
, but in order to do this thread-safely, I need to lock a readers-writer mutex:
const Value Object::list() const {
ScopedRead lock(children_);
...
}
But this breaks because the compiler complains about "children_" being const
and such. I went up to the ScopedRead class and up to the RWMutex class (which children_
is a sub-class) to allow read_lock
on a const object, but I have to write this:
inline void read_lock() const {
pthread_rwlock_rdlock(const_cast<pthread_rwlock_t*>(&rwlock_));
}
I have always learned that const_cast
is a code smell. Any way to avoid this ?