경고 C26138

잠금 '잠금'을 유지하는 동안 코루틴을 일시 중단합니다.

설명

잠금을 유지하는 동안 코루틴이 일시 중단되면 경고 C26138이 경고합니다. 일반적으로 코루틴이 일시 중단된 상태에서 얼마나 오래 재기본지정될지 알 수 없으므로 이 패턴으로 인해 예상보다 긴 중요한 섹션이 발생할 수 있습니다.

코드 분석 이름: SUSPENDED_WITH_LOCK

예제

다음 코드는 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();
}

다음 코드에서는 이러한 경고가 해결됩니다.

#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.
}