c++ - std::call_once 似乎不会在发生异常时释放锁?
问题描述
根据cppreference.com的说法,std::call_once
应该可以与可能引发异常的函数一起正常工作。话虽如此,测试那里表示的示例会导致无限等待而不是加入所有线程。
上面页面中的示例:
#include <iostream>
#include <thread>
#include <mutex>
std::once_flag flag1, flag2;
void simple_do_once()
{
std::call_once(flag1, [](){ std::cout << "Simple example: called once\n"; });
}
void may_throw_function(bool do_throw)
{
if (do_throw) {
std::cout << "throw: call_once will retry\n"; // this may appear more than once
throw std::exception();
}
std::cout << "Didn't throw, call_once will not attempt again\n"; // guaranteed once
}
void do_once(bool do_throw)
{
try {
std::call_once(flag2, may_throw_function, do_throw);
}
catch (...) {
}
}
int main()
{
std::thread st1(simple_do_once);
std::thread st2(simple_do_once);
std::thread st3(simple_do_once);
std::thread st4(simple_do_once);
st1.join();
st2.join();
st3.join();
st4.join();
std::thread t1(do_once, true);
std::thread t2(do_once, true);
std::thread t3(do_once, false);
std::thread t4(do_once, true);
t1.join();
t2.join();
t3.join();
t4.join();
}
在我的情况下,它会在以下输出之后导致无限等待:
Simple example: called once
throw: call_once will retry
对于以下所有版本,结果是相同的。
铿锵++
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
克++
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
g++-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
这意味着什么?
解决方案
推荐阅读
- r - 在运行循环时将预测导出到数据集
- vb.net - 如何清除 DataGridView 的标题?
- r - 如何查看或访问 R 中因子变量标签后面的值(级别)?
- r - 如何在 R 中运行截断和膨胀的泊松模型?
- java - 服务可以在 Spring Boot 中更新控制器以进行长进程吗
- sql - 子字符串前后的 Oracle 正则表达式 n 个字符,不包括空格
- flutter - 使用 MultipartRequest 通过 HTTP 上传多个图像/视频文件
- c - c中关于掩码的按位运算
- laravel - Laravel Redis Predis 和方法 GET
- c - 我的复数代码收到“预期表达式”错误