Medium
std::jthread destruction when the worker never checks a stop token
badWorker() takes no std::stop_token parameter and never checks any cancellation signal. What happens when the enclosing block ends and t's destructor runs?
main.cpp
void badWorker() {
while (true) {
doWork();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int main() {
{
std::jthread t(badWorker);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
} // t's destructor runs here
std::cout << "main continues" << std::endl;
}24 of 51 in Concurrency