Main loop helper functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gamehack

    Main loop helper functions

    Hello all,

    I've thought about having to implement 2 functions, say
    enter_main_loop (func_name); and a function called quit_applicatio n();
    which terminates the program. The enter_main_loop () can be defined as
    something like:
    int enter_main_loop (func_name)
    {
    while(1)
    {
    func_name();
    }

    return 0;
    }

    But then how am I going to implement the quit_applicatio n(); ? The only
    way I thought it could be handled is changing enter_main_loop to
    something like:
    int terminate = 0;

    int enter_main_loop (func_name)
    {
    if(!terminate)
    func_name();

    return 0;
    }

    and then quit_applicatio n(); can simply do
    int quit_applicatio n()
    {
    return (terminate = 1);
    }

    What do you think about this approach? Any better ways of achieving the
    same effect? I'm planning to use the quit_applicatio n() in a
    multithreaded environment so I assume it should be thread-safe (but it
    isn't now, is it)?

    Thanks

  • Vladimir S. Oka

    #2
    Re: Main loop helper functions

    gamehack wrote:
    [color=blue]
    > Hello all,
    >
    > I've thought about having to implement 2 functions, say
    > enter_main_loop (func_name); and a function called quit_applicatio n();
    > which terminates the program. The enter_main_loop () can be defined as
    > something like:
    > int enter_main_loop (func_name)[/color]

    This might as well be void enter_main_loop (...) as it never returns.
    [color=blue]
    > {
    > while(1)
    > {
    > func_name();
    > }
    >[/color]

    Making this redundant as well.
    [color=blue]
    > return 0;
    > }
    >
    > But then how am I going to implement the quit_applicatio n(); ? The
    > only way I thought it could be handled is changing enter_main_loop to
    > something like:
    > int terminate = 0;
    >
    > int enter_main_loop (func_name)
    > {
    > if(!terminate)
    > func_name();
    >
    > return 0;
    > }[/color]

    This is not at all same as your first example, as it doesn't loop.
    [color=blue]
    >
    > and then quit_applicatio n(); can simply do
    > int quit_applicatio n()
    > {
    > return (terminate = 1);
    > }[/color]

    Where would you call this from (and why)?
    [color=blue]
    >
    > What do you think about this approach? Any better ways of achieving
    > the same effect? I'm planning to use the quit_applicatio n() in a
    > multithreaded environment so I assume it should be thread-safe (but it
    > isn't now, is it)?[/color]

    I think you're on more-or-less right track. Could this be what you want:

    #include <stdlib.h>

    #define QUIT_LOOPIN (1)
    #define KEEP_LOOPIN !(QUIT_LOOPIN)

    int func_name(/* whatever */);
    int quit_applicatio n(/* whatever */);
    void enter_main_loop (/* whatever */);

    int main(void)
    {
    enter_main_loop (/* whatever */);

    return quit_applicatio n(/* whatever */);
    }

    void enter_main_loop (/* whatever */)
    {
    while (1)
    {
    if (QUIT_LOOPIN == func_name()) return;
    }
    }

    int func_name(/* whatever */)
    {
    int time_to_quit = KEEP_LOOPIN;

    /* do stuff possibly changing time_to_quit value */

    return time_to_quit;
    }

    int quit_applicatio n(/* whatever */)
    {
    int success = EXIT_SUCCESS;

    /* do cleanup stuff possibly setting success = EXIT_FAILURE */

    return success;
    }

    Cheers

    Vladimir

    --
    In Seattle, Washington, it is illegal to carry a concealed weapon that
    is over six feet in length.

    Comment

    • gamehack

      #3
      Re: Main loop helper functions


      Vladimir S. Oka wrote:[color=blue]
      > gamehack wrote:
      >[color=green]
      > > Hello all,
      > >
      > > I've thought about having to implement 2 functions, say
      > > enter_main_loop (func_name); and a function called quit_applicatio n();
      > > which terminates the program. The enter_main_loop () can be defined as
      > > something like:
      > > int enter_main_loop (func_name)[/color]
      >
      > This might as well be void enter_main_loop (...) as it never returns.
      >[color=green]
      > > {
      > > while(1)
      > > {
      > > func_name();
      > > }
      > >[/color]
      >
      > Making this redundant as well.
      >[color=green]
      > > return 0;
      > > }
      > >
      > > But then how am I going to implement the quit_applicatio n(); ? The
      > > only way I thought it could be handled is changing enter_main_loop to
      > > something like:
      > > int terminate = 0;
      > >
      > > int enter_main_loop (func_name)
      > > {
      > > if(!terminate)
      > > func_name();
      > >
      > > return 0;
      > > }[/color]
      >
      > This is not at all same as your first example, as it doesn't loop.
      >[color=green]
      > >
      > > and then quit_applicatio n(); can simply do
      > > int quit_applicatio n()
      > > {
      > > return (terminate = 1);
      > > }[/color]
      >
      > Where would you call this from (and why)?
      >[color=green]
      > >
      > > What do you think about this approach? Any better ways of achieving
      > > the same effect? I'm planning to use the quit_applicatio n() in a
      > > multithreaded environment so I assume it should be thread-safe (but it
      > > isn't now, is it)?[/color]
      >
      > I think you're on more-or-less right track. Could this be what you want:
      >
      > #include <stdlib.h>
      >
      > #define QUIT_LOOPIN (1)
      > #define KEEP_LOOPIN !(QUIT_LOOPIN)
      >
      > int func_name(/* whatever */);
      > int quit_applicatio n(/* whatever */);
      > void enter_main_loop (/* whatever */);
      >
      > int main(void)
      > {
      > enter_main_loop (/* whatever */);
      >
      > return quit_applicatio n(/* whatever */);
      > }
      >
      > void enter_main_loop (/* whatever */)
      > {
      > while (1)
      > {
      > if (QUIT_LOOPIN == func_name()) return;
      > }
      > }
      >
      > int func_name(/* whatever */)
      > {
      > int time_to_quit = KEEP_LOOPIN;
      >
      > /* do stuff possibly changing time_to_quit value */
      >
      > return time_to_quit;
      > }
      >
      > int quit_applicatio n(/* whatever */)
      > {
      > int success = EXIT_SUCCESS;
      >
      > /* do cleanup stuff possibly setting success = EXIT_FAILURE */
      >
      > return success;
      > }
      >
      > Cheers
      >
      > Vladimir
      >
      > --
      > In Seattle, Washington, it is illegal to carry a concealed weapon that
      > is over six feet in length.[/color]

      This is what I meant:

      int func_name();
      void enter_main_loop (func_name);
      void quit_main_loop( void);

      int terminate = 0;

      void enter_main_loop (func_name)
      {
      while(1)
      {
      if(terminate != 0)
      return;
      else
      {
      func_name();
      }
      }
      }


      void quit_main_loop( void)
      {
      terminate = 1;
      }

      and then in main.c:

      int main(int argc, char* argv[])
      {
      enter_main_loop (func_name);

      return 0;
      }

      The only problem I can see is that func_name() can hold the termination
      if it is synchronous. The func_name() function will be responsible for
      making async calls(implement ed with threads) to other sync functions
      which operate on a call queue so I can call quit_main_loop( ); from
      anywhere and it will terminate the program without waiting for some
      function to finish doing its work.

      Thanks

      Comment

      • Vladimir S. Oka

        #4
        Re: Main loop helper functions

        gamehack wrote:
        [color=blue]
        >
        > Vladimir S. Oka wrote:[color=green]
        >> gamehack wrote:
        >>[color=darkred]
        >> > Hello all,[/color][/color][/color]

        <snip OP's first code snippet>
        [color=blue][color=green][color=darkred]
        >> >
        >> > What do you think about this approach? Any better ways of achieving
        >> > the same effect? I'm planning to use the quit_applicatio n() in a
        >> > multithreaded environment so I assume it should be thread-safe (but
        >> > it isn't now, is it)?[/color]
        >>
        >> I think you're on more-or-less right track. Could this be what you
        >> want:
        >>
        >> #include <stdlib.h>
        >>
        >> #define QUIT_LOOPIN (1)
        >> #define KEEP_LOOPIN !(QUIT_LOOPIN)
        >>
        >> int func_name(/* whatever */);
        >> int quit_applicatio n(/* whatever */);
        >> void enter_main_loop (/* whatever */);
        >>
        >> int main(void)
        >> {
        >> enter_main_loop (/* whatever */);
        >>
        >> return quit_applicatio n(/* whatever */);
        >> }
        >>
        >> void enter_main_loop (/* whatever */)
        >> {
        >> while (1)
        >> {
        >> if (QUIT_LOOPIN == func_name()) return;
        >> }
        >> }
        >>
        >> int func_name(/* whatever */)
        >> {
        >> int time_to_quit = KEEP_LOOPIN;
        >>
        >> /* do stuff possibly changing time_to_quit value */
        >>
        >> return time_to_quit;
        >> }
        >>
        >> int quit_applicatio n(/* whatever */)
        >> {
        >> int success = EXIT_SUCCESS;
        >>
        >> /* do cleanup stuff possibly setting success = EXIT_FAILURE */
        >>
        >> return success;
        >> }
        >>
        >> Cheers
        >>
        >> Vladimir
        >>
        >> --
        >> In Seattle, Washington, it is illegal to carry a concealed weapon
        >> that is over six feet in length.[/color]
        >
        > This is what I meant:
        >
        > int func_name();
        > void enter_main_loop (func_name);
        > void quit_main_loop( void);
        >
        > int terminate = 0;
        >
        > void enter_main_loop (func_name)
        > {
        > while(1)
        > {
        > if(terminate != 0)
        > return;
        > else
        > {
        > func_name();
        > }
        > }
        > }
        >
        >
        > void quit_main_loop( void)
        > {
        > terminate = 1;
        > }
        >
        > and then in main.c:
        >
        > int main(int argc, char* argv[])
        > {
        > enter_main_loop (func_name);
        >
        > return 0;
        > }
        >
        > The only problem I can see is that func_name() can hold the
        > termination if it is synchronous. The func_name() function will be
        > responsible for making async calls(implement ed with threads) to other
        > sync functions which operate on a call queue so I can call
        > quit_main_loop( ); from anywhere and it will terminate the program
        > without waiting for some function to finish doing its work.[/color]

        You never call quit_main_loop( ) so your loop never ends.

        If you're thinking of multi-threaded execution that is not supported by
        Standard C, and is hence off topic here.

        In my code above, you could call quit_main_loop( ) from func_name() to
        determine whether it's time to quit. I misunderstood that
        quit_applicatio n() does pre-exit clean-up.

        Cheers

        Vladimir

        Comment

        • Herbert Rosenau

          #5
          Re: Main loop helper functions

          On Sat, 28 Jan 2006 16:09:16 UTC, "gamehack" <gamehack@gmail .com>
          wrote:
          [color=blue]
          > This is what I meant:
          >
          > int func_name();
          > void enter_main_loop (func_name);
          > void quit_main_loop( void);[/color]

          volatile int terminate = 0; /* thread save */[color=blue]
          >
          > void enter_main_loop (func_name)
          > {
          > while(!terminat e)
          > {
          > func_name();
          > }
          > }
          >
          >
          > void quit_main_loop( void)
          > {
          > terminate = 1;
          > }
          >
          > and then in main.c:
          >
          > int main(int argc, char* argv[])
          > {
          > enter_main_loop (func_name);
          >
          > return 0;
          > }
          >
          > The only problem I can see is that func_name() can hold the termination
          > if it is synchronous. The func_name() function will be responsible for
          > making async calls(implement ed with threads) to other sync functions
          > which operate on a call queue so I can call quit_main_loop( ); from
          > anywhere and it will terminate the program without waiting for some
          > function to finish doing its work.
          >
          > Thanks
          >[/color]

          For your problem the standard defines volatile - even C knows nothing
          about threads.
          So whenever a thread needs to quit the whole app it will change
          terminate to not be 0 (it is on quit_main_loop to determine that all
          threads are ready to let main (and themrself die.

          --
          Tschau/Bye
          Herbert

          Visit http://www.ecomstation.de the home of german eComStation
          eComStation 1.2 Deutsch ist da!

          Comment

          • Malcolm

            #6
            Re: Main loop helper functions


            "gamehack" <gamehack@gmail .com> wrote[color=blue]
            > I've thought about having to implement 2 functions, say
            > enter_main_loop (func_name); and a function called quit_applicatio n();
            > which terminates the program. The enter_main_loop () can be defined as
            > something like:
            > int enter_main_loop (func_name)
            > {
            > while(1)
            > {
            > func_name();
            > }
            >
            > return 0;
            > }
            >
            > But then how am I going to implement the quit_applicatio n(); ? The only
            > way I thought it could be handled is changing enter_main_loop to
            > something like:
            > int terminate = 0;
            >
            > int enter_main_loop (func_name)
            > {
            > if(!terminate)
            > func_name();
            >
            > return 0;
            > }
            >
            > and then quit_applicatio n(); can simply do
            > int quit_applicatio n()
            > {
            > return (terminate = 1);
            > }
            >
            > What do you think about this approach? Any better ways of achieving the
            > same effect? I'm planning to use the quit_applicatio n() in a
            > multithreaded environment so I assume it should be thread-safe (but it
            > isn't now, is it)?
            >[/color]
            Use setjmp() and lngjmp().

            Otherwise there is no option than to make every function in your program
            return a "terminate" flag.


            Comment

            • Vladimir S. Oka

              #7
              Re: Main loop helper functions

              Malcolm wrote:
              [color=blue]
              >
              > "gamehack" <gamehack@gmail .com> wrote[color=green]
              >> What do you think about this approach? Any better ways of achieving
              >> the same effect? I'm planning to use the quit_applicatio n() in a
              >> multithreaded environment so I assume it should be thread-safe (but
              >> it isn't now, is it)?
              >>[/color]
              > Use setjmp() and lngjmp().
              >
              > Otherwise there is no option than to make every function in your
              > program return a "terminate" flag.[/color]

              <OT>
              I don't think setjmp() and lngjmp() are thread safe either. I don't
              think they're very safe/good practice either, threads or no threads.
              </OT>

              --
              A classic is something that everybody wants to have read and nobody
              wants to read.
              -- Mark Twain

              Comment

              Working...