sleep and wait

This is a discussion on sleep and wait within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to have my program A which contains the main function wait on a signal from a function ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    sleep and wait

    Hi,
    I need to have my program A which contains the main function wait on a signal from a function from a class B so that the main function can fall through to the return statement to end the program.

    I don't want to use a while loop though. I want program A to sleep and stop all activity so that it does not consume cpu time and then whenever object B needs to end the program, it would signal A somehow. I know how to use callback functions but how would the function access things inside the main function??? Any idea how to do this. Thanks a bunch.
    Amish

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    If your program is multithreaded, then the techniques are system dependent. You'll need to identify your compiler and operating system, and someone may be able to give you some useful advice on what functions to call, etc.

    If your program is single threaded, then the function in class B needs to return (as do all the functions called by main that eventually call the function in class B).

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    The function that main waits on is a callback function. So main cannot call it. It gets called from another object.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    if you're using windows look into WaitForSingleObject
    and Setevent. That should get you started
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Quote Originally Posted by axr0284
    The function that main waits on is a callback function. So main cannot call it. It gets called from another object.
    I sort of guessed that. But callback functions are not necessarily invoked in different threads from main(). The scheme by which you will pass control back to main would therefore depend on the workings of your callback system.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    If the timing is not too critical, you can "compromise" by putting a delay (maybe 10 - 100 milliseconds) in your wait-loop. To the CPU, this is a long delay. Your loop will consume almost no CPU cycles, but the delay will be very short in human-time.

    On a multitasking system, it is common practice (and good practice) to put a sleep-delay in your "do-nothing loops." It helps your program "play well with others."
    Last edited by DougDbug; 01-06-2006 at 10:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  2. making my program sleep / wait / delay...
    By bobthebullet990 in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2006, 10:14 AM
  3. Sleep question and a bug I can't figure out
    By ac251404 in forum Windows Programming
    Replies: 10
    Last Post: 08-08-2006, 08:24 AM
  4. sleep function
    By osal in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2005, 12:09 PM
  5. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 09:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21