Thread: Looping once a second...

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    Looping once a second...

    My program keeps the time since it was started every microsecond. I want to preform an action at a certain amount of microseconds after the program is started. I need to process these actions many times, once a second. I thought I might do this with a loop, but I cant put actions into a normal loop because quite obviously it would process these actions at different speeds on different computers.

    I hope you understand what I am trying to achieve, and could I prehaps get some little help?

    Many thanks in advance.

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Code:
    for loop{
    
    do something
    sleep(1000); /sleep for 1 sec
    
    }

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    'sleep()' is declared in 'dos.h' by the way
    Last edited by Brain Cell; 11-30-2004 at 10:30 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>'sleep()' is declared in 'dos.h' by the way
    Alternately, Sleep() is declared in <windows.h>.

    Or, you could do something abhorrent and sinister in nature, by making your own sleep function using the standard time functions and running a dead loop until a certain amount of time has elapsed.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > My program keeps the time since it was started every microsecond.
    That kind of stuff is very dependent on your OS and compiler.

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Hunter2
    >>'sleep()' is declared in 'dos.h' by the way
    Alternately, Sleep() is declared in <windows.h>.

    Or, you could do something abhorrent and sinister in nature, by making your own sleep function using the standard time functions and running a dead loop until a certain amount of time has elapsed.
    running a dead loop uses processing power.. where as putting it to sleep will make sure that the processor is used for other purpose during the sleep period....

  7. #7
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    Sleep is messy. If the user cancels the loop or something, no, its just horrible.

    There must be another way. I'm compiling with GCC anyway.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I'm compiling with GCC anyway.
    OK, but gcc is available for many operating systems....

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    On GNU it's in unistd.h and it's not sleeping microns but seconds. So sleep(1000) is quite a bad idea
    There is also nanosleep() in the same header - doing what it's name implies.

    Alternatively you could use the SIGALRM to signal your program every second.
    It's documented in the reference here and here

    Again, that's for GNU Glibc systems. You didn't mention yours.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xconspirisist
    Sleep is messy. If the user cancels the loop or something, no, its just horrible.
    Well that's an entirely different question isn't it? Shouldn't you be asking:

    "How can I loop once per second, while continually checking for a possible interrupt from the user to cancel the loop?"

    Different ball game all together.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Quote Originally Posted by vasanth
    running a dead loop uses processing power.. where as putting it to sleep will make sure that the processor is used for other purpose during the sleep period....
    Quote Originally Posted by Hunter2
    Or, you could do something abhorrent and sinister in nature, by making your own sleep function using the standard time functions and running a dead loop until a certain amount of time has elapsed.
    Was I unfair?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    I will seek help elsewhere.

  13. #13
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by xconspirisist
    I will seek help elsewhere.
    you are welcome....

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by xconspirisist
    I will seek help elsewhere.
    Glad we could help.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I will seek help elsewhere.
    Well if you're going to be vague about what you want, and don't give people the information they ask for, then all I can say is good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM