Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在多线程应用程序中优雅地从断言失败中退出_C++_C++11_Assert - Fatal编程技术网

C++ 如何在多线程应用程序中优雅地从断言失败中退出

C++ 如何在多线程应用程序中优雅地从断言失败中退出,c++,c++11,assert,C++,C++11,Assert,在多线程应用程序中,我使用以下断言宏捕获调试单元测试期间的编码和数据错误 #ifndef NDEBUG #define ASSERT(condition, message) \ do { \ if (! (condition)) { \ std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \ << " line " << __LIN

在多线程应用程序中,我使用以下断言宏捕获调试单元测试期间的编码和数据错误

#ifndef NDEBUG
#define ASSERT(condition, message) \
do { \
    if (! (condition)) { \
        std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
        << " line " << __LINE__ << ": " << message << std::endl; \
        std::exit( EXIT_FAILURE ); \
    } \
} while (false)
#else
#define ASSERT(condition, message) do { } while( false )
#endif
\ifndef NDEBUG
#定义断言(条件、消息)\
做{\
如果(!(条件)){\

std::cerr虽然它既快又脏,
\u exit
在大多数平台上都能很好地工作。

是的,抛出一个异常。不过,请阅读多线程环境中的异常传播。@KerrekSB-ty-I将开始在topic@KerrekSB:引发异常的问题是,在捕获异常时错误文本丢失,这使得确定错误原因变得更加困难。@Mankarse:我认为错误消息负责打印出必要的上下文信息。+1 ty-这似乎有效-你能详细说明一下使用_exit()时实际发生的情况吗?(我也会用谷歌搜索)如果我能理解它的局限性,我可能会在其他情况下使用这个调用。它会半立即、无条件、不优雅地终止调用过程。