Thread: Wait for callback without quitting

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Wait for callback without quitting

    Hey,

    I am writing a program that is reading stock prices through a callback function and then doing something when I get prices. Basically I am subscribing to the stock symbols in my main function and then waiting for prices.

    How do I get my program not to quit when my main function ends but I am still waiting for a callback function to be called several times? I never want the program to end until I exit it through ctrl + c.

    thanks
    -Jeff

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would depend on how you subscribe "to the stock symbols" and register the callback functions in the first place. You are presumably using a third-party library of some type (such functionality is not generally part of standard C or C++) to do those things, and such things will be specific to that library. Read the documentation for that library to determine how to wait for all callbacks to be completed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    All callbacks are never completed though. The connection to the price feed remains open until I close it. I never want it to stop listening for prices.

    There is not really any documentation for what I am looking for in this API....

    Is there just a simple way to leave the program running?

    Thanks

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    If you just want to close the program with Ctrl+C, then keep the program running in an infinite loop.

    Code:
    while(1)
    {
    // stuff
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by syzygy View Post
    If you just want to close the program with Ctrl+C, then keep the program running in an infinite loop.
    Ditto. This is called an "event loop" or a "main loop" (or a "main event loop") and that's how most interactive real time applications (such as GUI's and games) work.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    If I use the while loop, and I am running a lot of stuff on callbacks, with this hurt performance?

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    So long as you aren't doing needless processing inside the main loop (the infinite one), then there shouldn't be any performance problems. As MK27 said, it's how real-time apps function.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Depends on how it is all set up. You do not want to just do this:
    Code:
    while (1);
    Or something effectively the same:
    Code:
    while (1) {
         [check some condition, possibly to exit]
    }
    This will just max out a processor unnecessarily.

    With GUI libraries, the main loop is an integral part. Not knowing anything about the library you are using, it's impossible for me to say how you could integrate it. One way might to be to register one of your callbacks in a kind of blocking mode if possible.

    Or, a simple solution:
    Code:
    while (1) {
         [check some condition, or not]
         sleep(1);
    }
    Sleep() is what's called a "passive" sleep, meaning the processor doesn't have to do anything/can do other things. A loop with a sleep(1) (or other appropriate passive gap) in it will use very little processor time, whereas one without such will try to revolve as fast as possible (that's why "while(1);" will just hog the whole thing doing nothing, like holding your foot down on the accelerator while in neutral). So what syzygy says ("as long as you aren't doing needless processing") is untrue -- do no processing will just cause the loop to execute a billion times a second, which means, in effect, a lot of processing. You have to ensure the loop does not repeat without some kind of blocking operation or a delay. Blocking operations would be I think normative, altho games (for example) can use short sleep type delays to maintain a smooth frame rate.

    "Active sleeps" are I think almost never necessary in userspace programming.

    If your program is working fine and dandy already, you could just stick:
    Code:
    while(sleep(1));
    at the end of main().
    Last edited by MK27; 06-24-2010 at 09:45 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Yeah, good point. I wasn't thinking about that.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    If your program is working fine and dandy already, you could just stick:
    Code:
    while(sleep(1));
    at the end of main().

    From the sleep() manual:
    RETURN VALUE
    Zero if the requested time has elapsed, or the number of seconds left
    to sleep, if the call was interrupted by a signal handler.
    That is, sleep will return 0 when successfully sleeping. So your while(sleep(1)); won't work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why wait cursor still turning on?
    By Opariti in forum Windows Programming
    Replies: 0
    Last Post: 05-22-2009, 02:28 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Signals, fork(), wait() and exec()
    By DJTurboToJo in forum C Programming
    Replies: 7
    Last Post: 03-18-2008, 09:14 AM
  4. Boom, Headoshot!!
    By mrafcho001 in forum A Brief History of Cprogramming.com
    Replies: 50
    Last Post: 07-21-2005, 08:28 PM
  5. anybody with experience using the wait() function?
    By flawildcat in forum C Programming
    Replies: 7
    Last Post: 04-22-2002, 02:43 PM