1.. title:: clang-tidy - cppcoreguidelines-no-suspend-with-lock 2 3cppcoreguidelines-no-suspend-with-lock 4====================================== 5 6Flags coroutines that suspend while a lock guard is in scope at the 7suspension point. 8 9When a coroutine suspends, any mutexes held by the coroutine will remain 10locked until the coroutine resumes and eventually destructs the lock guard. 11This can lead to long periods with a mutex held and runs the risk of deadlock. 12 13Instead, locks should be released before suspending a coroutine. 14 15This check only checks suspending coroutines while a lock_guard is in scope; 16it does not consider manual locking or unlocking of mutexes, e.g., through 17calls to ``std::mutex::lock()``. 18 19Examples: 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 38This check implements `CP.52 39<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rcoro-locks>`_ 40from the C++ Core Guidelines. 41