--- description: "Learn more about: Warning C26138" title: Warning C26138 ms.date: 01/14/2019 f1_keywords: ["C26138", "SUSPENDED_WITH_LOCK"] helpviewer_keywords: ["C26138"] --- # Warning C26138 > Suspending a coroutine while holding lock '*lock*'. ## Remarks Warning C26138 warns when a coroutine is suspended while holding a lock. In general, we can't know how long will a coroutine remain in the suspended state so this pattern may result in longer critical sections than expected. Code analysis name: `SUSPENDED_WITH_LOCK` ## Examples The following code will generate C26138. ```cpp #include #include #include using namespace std::experimental; std::mutex global_m; _Guarded_by_(global_m) int var = 0; generator mutex_acquiring_generator() { global_m.lock(); ++var; co_yield 1; // @expected(26138), global_m is hold while yielding. global_m.unlock(); } generator mutex_acquiring_generator_report_once() { global_m.lock(); ++var; co_yield 1; // @expected(26138), global_m is hold while yielding. co_yield 1; // @expected(26138), global_m is hold while yielding. global_m.unlock(); } ``` The following code will correct these warnings. ```cpp #include #include #include using namespace std::experimental; std::mutex global_m; _Guarded_by_(global_m) int var = 0; generator mutex_acquiring_generator2() { { global_m.lock(); ++var; global_m.unlock(); } co_yield 1; // no 26138, global_m is already released above. } ```