我写了一个小测试项目,看看在执行callable时std :: call_once是否阻塞。项目的输出允许假设call_once有2个行为:它在分离的线程上阻塞,而在join上不阻塞。我强烈怀疑这不可能成立,但是我无法得出其他结论,请指导我做出正确的结论。
using namespace std;
once_flag f;
mutex cout_sync;
void my_pause()
{
volatile int x = 0;
for(int i=0; i<2'000'000'000; ++i) { x++; }
}
void thr(int id)
{
auto start = chrono::system_clock::now();
call_once(f, my_pause);
auto end = chrono::system_clock::now();
scoped_lock l{cout_sync};
cout << "Thread " << id << " finished in " << (static_cast<chrono::duration<double>>(end-start)).count() << " sec" << endl;
}
int main()
{
vector<thread> threads;
for(int i=0; i<4; …Run Code Online (Sandbox Code Playgroud) 在C ++项目上工作时,我发现VS19输出非常无用。考虑在新安装的VS19上运行示例代码:
#include <iostream>
using namespace std;
class My
{
public:
void f() noexcept
{
throw exception{"A problem sir!"};
}
};
int main()
{
try
{
My m;
m.f();
}
catch (exception& ex)
{
cout << "exception caught! " << ex.what() << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望收到的是:“函数在标记为noexcept时抛出异常”,并且将光标设置在有问题的行上。我得到的是一个带有一些常规文本的新窗口,没有一个提到问题或问题所在。