Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mutexes mutable to preserve function constness #655

Open
grahamreeds opened this issue Jul 28, 2016 · 3 comments
Open

Make mutexes mutable to preserve function constness #655

grahamreeds opened this issue Jul 28, 2016 · 3 comments
Assignees

Comments

@grahamreeds
Copy link

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_;
@cubbimew
Copy link
Member

cubbimew commented Aug 2, 2016

That's @hsutter 's "M&M rule" (mutable and mutex go together): https://herbsutter.com/2013/05/24/gotw-6a-const-correctness-part-1-3/

@AndrewPardoe
Copy link
Contributor

Herb, we should consider a "use mutable when" rule. Sergey, could you please write up a rule about objects that are internally synchronized--mutex, atomic, lock-free queue--and submit it as a PR?

Bjarne suggests need two rules: application for synchronization and when you want a mutable.

@catskul
Copy link

catskul commented May 17, 2021

Is this still being considered for inclusion? @hsutter @AndrewPardoe @cubbimew ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants