Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 1.58 KB

File metadata and controls

70 lines (53 loc) · 1.58 KB
title C26138
ms.date 01/14/2019
ms.topic reference
f1_keywords
C26138
helpviewer_keywords
C26138
author sunnychatterjee
ms.author sunnych

C26138

warning C26138: Suspending a coroutine while holding lock <lock>.

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.

Example

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();
}

Example

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