Search code examples
cucos

Execute a code only one time in C


I want execute a code only one time. I used a solution based on a global varibale of bool type. My question, is it the best solution?

Remark: I use ucos-II.

if (TRUE == Lock)
{
 /*Code to execute one time*/
}

/*The reste of the code*/

Solution

  • A simple code using a static variable.

    static bool once = false;
    
    if (once == false)
    {
        once = true;
        // do your "once" stuff here
    }