Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/no-suspend-with-lock.rst

raw
1 .. title:: clang-tidy - cppcoreguidelines-no-suspend-with-lock
2
3 cppcoreguidelines-no-suspend-with-lock
4 ======================================
5
6 Flags coroutines that suspend while a lock guard is in scope at the
7 suspension point.
8
9 When a coroutine suspends, any mutexes held by the coroutine will remain
10 locked until the coroutine resumes and eventually destructs the lock guard.
11 This can lead to long periods with a mutex held and runs the risk of deadlock.
12
13 Instead, locks should be released before suspending a coroutine.
14
15 This check only checks suspending coroutines while a lock_guard is in scope;
16 it does not consider manual locking or unlocking of mutexes, e.g., through
17 calls to ``std::mutex::lock()``.
18
19 Examples:
20
21 .. code-block:: c++
22
23 future bad_coro() {
24 std::lock_guard lock{mtx};
25 ++some_counter;
26 co_await something(); // Suspending while holding a mutex
27 }
28
29 future good_coro() {
30 {
31 std::lock_guard lock{mtx};
32 ++some_counter;
33 }
34 // Destroy the lock_guard to release the mutex before suspending the coroutine
35 co_await something(); // Suspending while holding a mutex
36 }
37
38 This check implements `CP.52
39 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rcoro-locks>`_
40 from the C++ Core Guidelines.