Skip to content

Latest commit

 

History

History
68 lines (52 loc) · 1.63 KB

c26138.md

File metadata and controls

68 lines (52 loc) · 1.63 KB
description title ms.date f1_keywords helpviewer_keywords
Learn more about: Warning C26138
Warning C26138
01/14/2019
C26138
SUSPENDED_WITH_LOCK
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.

#include <experimental/generator>
#include <future>
#include <mutex>

using namespace std::experimental;

std::mutex global_m;
_Guarded_by_(global_m) int var = 0;

generator<int> mutex_acquiring_generator() {
  global_m.lock();
  ++var;
  co_yield 1;                           // @expected(26138), global_m is hold while yielding.
  global_m.unlock();
}

generator<int> 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.

#include <experimental/generator>
#include <future>
#include <mutex>

using namespace std::experimental;

std::mutex global_m;
_Guarded_by_(global_m) int var = 0;

generator<int> mutex_acquiring_generator2() {
  {
    global_m.lock();
    ++var;
    global_m.unlock();
  }
  co_yield 1;                           // no 26138, global_m is already released above.
}