this post was submitted on 06 Jul 2023
13 points (100.0% liked)
C++
1783 readers
1 users here now
The center for all discussion and news regarding C++.
Rules
- Respect instance rules.
- Don't be a jerk.
- Please keep all posts related to C++.
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
std::lock_guard
and it's superior alternativestd::scoped_lock
both deliberately cannot be moved, so you won't be able to make it work with these types. But, assumingfoo
will cause the lambda to be destroyed once it is done executing you can use the more flexiblestd::unique_lock
and move that into the lambda:If the lambda does not capture anything whose destructor could call code that tries to lock this same mutex by value and
foo
destroys the lambda immediately after the lambda has finished executing then this does exactly what you want.