473,394 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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_application();
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_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;
}

and then quit_application(); can simply do
int quit_application()
{
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_application() in a
multithreaded environment so I assume it should be thread-safe (but it
isn't now, is it)?

Thanks

Jan 28 '06 #1
6 1678
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.

Jan 28 '06 #2

Vladimir S. Oka wrote:
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.


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.

Thanks

Jan 28 '06 #3
gamehack wrote:

Vladimir S. Oka wrote:
gamehack wrote:
> Hello all,
<snip OP'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_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.


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

Jan 28 '06 #4
On Sat, 28 Jan 2006 16:09:16 UTC, "gamehack" <ga******@gmail.com>
wrote:
This is what I meant:

int func_name();
void enter_main_loop(func_name);
void quit_main_loop(void);
volatile int terminate = 0; /* thread save */
void enter_main_loop(func_name)
{
while(!terminate)
{
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.

Thanks


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!
Jan 29 '06 #5

"gamehack" <ga******@gmail.com> wrote
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)
{
while(1)
{
func_name();
}

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;
}

and then quit_application(); can simply do
int quit_application()
{
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_application() in a
multithreaded environment so I assume it should be thread-safe (but it
isn't now, is it)?

Use setjmp() and lngjmp().

Otherwise there is no option than to make every function in your program
return a "terminate" flag.
Jan 29 '06 #6
Malcolm wrote:

"gamehack" <ga******@gmail.com> wrote
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)?

Use setjmp() and lngjmp().

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


<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

Jan 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.