0

I'm beginner with c and have a simple question:

I have a function myfunction() which is called periodically every 100 ms. Within this function I have to call an other function but only once at the first call at beginn of myfunction(), but no periodically.

void myfunction() // function is called periodically every 100 ms
{
    ...
    mySubfunction(); // this function have to be called only once in the  first call of myFunction() and than skipped each time after that.
}   ...

How to realize this in c?

JohnDoe
  • 825
  • 1
  • 13
  • 31
  • If `mySubfunction` in general is not supposed be called when `myfunction` is called, then `mySubfunction` shouldn't be part of `myfunction`! – mkrieger1 Mar 07 '17 at 12:16

4 Answers4

4

Use static? Something along the lines of

void myfunction() // function is called periodically every 100 ms
{
    static int once = 1;
    if (once) {
        mySubfunction(); 
        once = 0;
    }
}  

The variable once in the example will be initalized only once and retain its value between invocations because of static keyword.

Be aware of implications in multithreaded environment, see this question.

Community
  • 1
  • 1
lukeg
  • 4,189
  • 3
  • 19
  • 40
0

you can have something like

static int flag = 1
void myfunction() // function is called periodically every 100 ms
{
    if(flag)
    {
       mySubfunction(); 
       flag = 0;
    }     
 }   

...

Learner
  • 67
  • 7
0

on first look task is very simply, can be next code:

void func()
{
    static LONG first = TRUE;
    if (_InterlockedExchange(&first, FALSE))
    {
        subfunc();
    }
    // some code
}

this give 100% guarantee that subfunc() will be called once and only once even if several thread in concurrent call your func()

but what be if // some code depended on result of subfunc ? in this case task become already not trivial. need some synchronization. and here already depended from os or compiler. in Windows, begin from Vista understand this problem and add function InitOnceExecuteOnce - read Using One-Time Initialization if your subfunc() have no in and out parameters code can be very simply:

BOOL CALLBACK InitOnceCallback(PINIT_ONCE /*InitOnce*/, PVOID /*Parameter*/,PVOID* /*Context*/)
{
    subfunc();
    return TRUE;
}

void func()
{
    static INIT_ONCE once = RTL_RUN_ONCE_INIT;
    if (InitOnceExecuteOnce(&once, InitOnceCallback, 0, 0))
    {
        // somecode
    }
    // error init
}

also some modern compilers can correct handle static one time initialization. say latest versions of CL. with it code can be next:

void func()
{
    static char tag = (subfunc(), 0);
    // some code
}

here CL internally call special functions (implemented in CRT) _Init_thread_header, _Init_thread_footer - implementation can be look in crt source code - thread_safe_statics.cpp

RbMm
  • 31,280
  • 3
  • 35
  • 56
0

This may be more advanced than you're looking for, but you could use function pointers and change which function gets called.

// Function declarations
void mySubfunction(void);
void myNormalfunction(void);

// Function pointer, which can be changed at run time.
static void (*myfunction)(void) = mySubfunction;

void mySubfunction(void)
{
    // Do the sub-function stuff.

    // Change the function pointer to the normal function.
    myfunction = myNormalfunction();

    // Do the normal function stuff (if necessary on the first call).
    myNormalfunction();
}

void myNormalfunction(void)
{
    // Etc.
}

int main(void)
{
    int x;
    for(x = 0; x < 3; x++)
    {
        // Call myfunction as you usually would.
        myfunction();
    }
    return 0;
}
Evil Dog Pie
  • 2,300
  • 2
  • 23
  • 46