41 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-no-suspend-with-lock2 3cppcoreguidelines-no-suspend-with-lock4======================================5 6Flags coroutines that suspend while a lock guard is in scope at the7suspension point.8 9When a coroutine suspends, any mutexes held by the coroutine will remain10locked 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., through17calls 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 mutex27 }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 coroutine35 co_await something(); // Suspending while holding a mutex36 }37 38This check implements `CP.5239<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rcoro-locks>`_40from the C++ Core Guidelines.41