News for hackers. Stuff that matters.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by eps on Thursday April 06, 2023 @02:39AM (#35464978)
A pattern for easier slower concurrency.

Lambdas are expensive, so if the code under the mutex is trivial, the cost of using this sort of pattern will be noticeable. Especially in fast paths.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by ot on Thursday April 06, 2023 @02:52AM (#35465055)
Lambdas are free. Unless the class type-erases the callback (and MutexProtected doesn't), and the callback is not huge, the compiler is virtually guaranteed to inline it.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by huhtenberg on Thursday April 06, 2023 @09:55AM (#35468348)
It can go both ways.

Clang will inline everything - https://godbolt.org/z/v19P1W9sj

MSVC will not, even with O2 - https://godbolt.org/z/naovTxncs

> Might be worth looking at the resulting assembly first if you are looking to adapt this in your codebase.

Re:MutexProtected: A C++ Pattern for Easier Concurrency by akling on Thursday April 06, 2023 @02:53AM (#35465065)
AFAIK if you use a templated type for the callback (as you should), it will be inlined by modern compilers at any optimization level other than -O0. :)
Re:MutexProtected: A C++ Pattern for Easier Concurrency by gpderetta on Thursday April 06, 2023 @03:12AM (#35465238)
Aside from lambdas being cheap abstractions in C++, any code under a mutex is likely not the fast path anyway.
Re:MutexProtected: A C++ Pattern for Easier Concurrency by saagarjha on Thursday April 06, 2023 @03:33AM (#35465403)
I mean, some code is all slow paths ;)