并发断言 及时断言区别
Assertions are very helpful to find problems or bugs. But, sometimes there's a problem
断言对于发现问题或错误非常有帮助。 但是,有时候会有问题
using them, for example within a loop or a very often called function, because if such an
使用它们,例如在循环中或经常调用的函数中使用,因为如果
assertion fails it may happen that tons of assertion-dialogs pop up (at least in VC++
断言失败,可能会弹出大量断言对话框(至少在VC ++中)
with 'ASSERT'-macro that's quite a mess).
与“ ASSERT”宏非常混乱)。
The following macro can be used easily just the same way as VC++'s 'ASSERT'-macro or
可以很容易地使用以下宏,就像VC ++的“ ASSERT”宏或
CRT's 'assert'. To make its usage thread-safe first I introduce a simple class which does
CRT的“断言”。 为了使它的使用成为线程安全的,我首先介绍一个简单的类
synchronization using a mutex in Windows - if you want to use the macro with other
在Windows中使用互斥锁进行同步-如果要与其他宏一起使用
OS you'll have to adapt that class:
操作系统,您必须适应该类:
class Lock{HANDLEGetMutex(){static HANDLE hMutex = ::CreateMutex( NULL, FALSE, NULL );return hMutex;} public:Lock(){::WaitForSingleObject( GetMutex(), INFINITE );} ~Lock(){::ReleaseMutex( GetMutex() );}};Here's the macro itself:
这是宏本身:
// Handle the first time the assertion fails#define assert_once_out_first( x ) std::cout << __FILE__ << "(" << __LINE__ << "): first assertion for expression '" << #x << "'" << std::endl; // Handle further assertion fails#define assert_once_out_next( x ) std::cout << __FILE__ << "(" << __LINE__ << "): known assertion for expression '" << #x << "'" << std::endl; // Generates a file-wide unique variable name#define assert_once_var __bAsserted__ ## __LINE__ ## __ // The unique assert macro#define assert_once( x ) do { Lock _lock; static bool assert_once_v = false; if ( !(x) ) { if ( !assert_once_v ) { assert_once_v = true; assert_once_out_first( x ); } else { assert_once_out_next( x ); } } } while ( 0 )The usage is just simple, i.e.:
用法很简单,即:
> int _tmain( int argc, char* argv[] )
> int _tmain(int argc,char * argv [])
> {
> {
> for ( int i = 0; i < 3; i++ )
>对于(int i = 0; i <3; i ++)
> {
> {
> assert_once( i < 2 );
> assert_once(i <2);
>
>
> for ( int j = 0; j < 3; j++ )
>对于(int j = 0; j <3; j ++)
> {
> {
> assert_once( j % 2 != 1 );
> assert_once(j%2!= 1);
> }
>}
> }
>}
>
>
>
>
> return -1;
>返回-1;
>}
>}
The output will be:
输出将是:
> c:testtestassert.cpp(15): first assertion for expression 'j % 2 != 1'
> c: test testassert.cpp(15) :表达式'j%2!= 1'的第一个断言
> c:testtestassert.cpp(15): known assertion for expression 'j % 2 != 1'
> c: test testassert.cpp(15) :表达式'j%2!= 1'的已知断言
> c:testtestassert.cpp(11): first assertion for expression 'i < 2'
> c: test testassert.cpp(11) :表达式'i <2'的第一个断言
> c:testtestassert.cpp(15): known assertion for expression 'j % 2 != 1'
> c: test testassert.cpp(15) :表达式'j%2!= 1'的已知断言
For VC++ it's even simple to use the macros ASSERT and TRACE by just replacing these
对于VC ++,只需替换这些宏即可使用ASSERT和TRACE宏
two macros as shown here:
两个宏,如下所示:
> #define assert_once_out_first( x )
> #define assert_once_out_first(x)
> TRACE( "%s(%d): Assertion for expression '%s'n", __FILE__, __LINE__, #x );
> TRACE(“%s(%d):表达式'%s' n”的断言,__FILE__,__LINE__,#x);
> ASSERT( x );
> ASSERT(x);
>
>
> #define assert_once_out_next( x )
> #define assert_once_out_next(x)
> TRACE( "%s(%d): Known assertion for expression '%s'n", __FILE__, __LINE__, #x );
> TRACE(“%s(%d):表达式'%s' n的已知断言,__FILE__,__LINE__,#x);
翻译自: https://www.experts-exchange.com/articles/237/Unique-assertions.html
并发断言 及时断言区别