Reputation: 12757
std::lock_guard
and std::unique_lock
interfaces look very similar, in their common part (constructors and destructor).
Why there is no hierarchical relationship between them?
Upvotes: 3
Views: 328
Reputation: 21156
They have non substitutable semantics:
lock_guard
is guaranteed to be locked through all of it's lifetime.
unique_lock
doesn't guarantee that so it doesn't follow the "IS A"-rule (unique_lock
cannot be a lock_guard
, as it offers fewer guarantees).
Also implementing unique_lock
based on lock_guard
wouldn't be trivial (maybe even impossible) for that reason.
Obviously the same is true the other way round: Although you can implement a lock_guard
in terms of a unique_lock
(private inheritance), lock_guard
doesn't provide the same functionality (lock()/unlock()
) as unique_lock
so it cannot be publicly derived from it.
Upvotes: 8