Skip to content

Make mutexes mutable to preserve function constness #655

@grahamreeds

Description

@grahamreeds

Something I have recently seen is people removing const from read only functions due to adding a mutex to control access and of course mutex changes state when used.

Example:

// rvo will use the vector move constructor...
std::vector<widget> get() const
{
    std::unique_lock<std::mutex> lk(cs_);
    std::vector<widget> copy(widgets_);
    return copy;
}

// in class definition
mutable std::mutex cs_;
std::vector<widget>widgets_;

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions