Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple assert for ordered non re-entrant calling?

I have two functions:

void prepare() and void finish() that will be called sequentially like:

prepare();
<do something>;
finish(); 
... 
prepare(); 
<do something>; 
finish();

I want to make a simple assertion to simply test that they are in fact being called this way and that they aren't being called concurrently or out-of-order in the application.

This application is a single-threaded application. This is a simple development/testing sanity check to make sure that these functions are being called in-order and that for whatever reason, they aren't being called concurrently. Furthermore, these assertions/sanity checks should be omitted from production code as performance is crucial!

would a simple assert() like this work best?

int test = 0;

void prepare() {
   assert(++test == 1);

   .
   .
   .
}

void finish() {
    assert(--test == 0);

    .
    .
    .
}
like image 553
chriskirk Avatar asked Dec 16 '22 17:12

chriskirk


1 Answers

You might want to change

int test = 0;

to

#ifndef NDEBUG
int test = 0;
#endif

to satisfy your requirement that "any code relating to this test should be omitted from production".

like image 166
Raedwald Avatar answered Dec 19 '22 07:12

Raedwald