gamehack wrote:
Hello all,
I've thought about having to implement 2 functions, say
enter_main_loop(func_name); and a function called quit_application();
which terminates the program. The enter_main_loop() can be defined as
something like:
int enter_main_loop(func_name)
This might as well be void enter_main_loop(...) as it never returns.
{
while(1)
{
func_name();
}
Making this redundant as well.
return 0;
}
But then how am I going to implement the quit_application(); ? 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;
}
This is not at all same as your first example, as it doesn't loop.
and then quit_application(); can simply do
int quit_application()
{
return (terminate = 1);
}
Where would you call this from (and why)?
What do you think about this approach? Any better ways of achieving
the same effect? I'm planning to use the quit_application() in a
multithreaded environment so I assume it should be thread-safe (but it
isn't now, is it)?
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_application(/* whatever */);
void enter_main_loop(/* whatever */);
int main(void)
{
enter_main_loop(/* whatever */);
return quit_application(/* 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_application(/* 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.