gamehack wrote:
Vladimir S. wrote:
>gamehack wrote:
>>
>Hello all,
<snip P's first code snippet>
>>
>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_LPIN (1)
>#define KEEP_LPIN !(QUIT_LPIN)
>>
>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_LPIN == func_name()) return;
>}
>}
>>
>int func_name(/* whatever */)
>{
>int time_to_quit = KEEP_LPIN;
>>
>/* 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.
>
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(implemented 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.
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_application() does pre-exit clean-up.
Cheers
Vladimir