checking infinately

This is a discussion on checking infinately within the C Programming forums, part of the General Programming Boards category; What would be the best way to test a condition infinately, I tried this but it shot up my cpu ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    checking infinately

    What would be the best way to test a condition infinately, I tried this but it shot up my cpu usage to 100 %

    Code:
    while(1) {
       if(condition)
          break;
    }
    The variable is a time variable and will only change once a second, so I put a system("sleep 1"); that fixed the cpu problem, but then you can't hit control-c to stop it. Is there a better way?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    You need to say which OS/Compiler you have for a better answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    depends on your programming style. Infinitely looping is something I steer cleer of if I can but I guess it all depends on your style of programming. Best thing to do is to test for the value of the variable at the in the while(conditions here) header, that way you always exit. if on the other hand you never exit then the problem will be with your programming logic.
    Hope that helps.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I use gnu/linux, if it helps.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    it would greatly help if you could tell us what condition you're testing.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I have a set time, and the current time(_t), everytime the loop iterates, it gets the current time (c_time = time(NULL)), then compares it to the set time. If they are the same, exit the loop. I put a sleep in there because the time won't change until the second is over, is there a better way?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    There are no such things as infinate loops in C. Eventually it will stop, like when the process is halted

    using
    Code:
    while(1)
    {
      if (something) 
        break;
    }
    is really bad form. Move the condition(s) into the while instead. This will make your code easier to read and manage.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Heh hate when I post at exactly the same time as someone else

    I have a set time, and the current time(_t), everytime the loop iterates, it gets the current time (c_time = time(NULL)), then compares it to the set time. If they are the same, exit the loop. I put a sleep in there because the time won't change until the second is over, is there a better way?
    Just thrown together:
    Code:
    #include <stdio.h> /* For printf */
    #include <time.h>  /* For time */
    #include <unistd.h> /* For sleep */
    
    int main (void)
    {
        time_t future=time(NULL) + 10, current;
    
        while ( (current = time(NULL)) < future)
        {
          printf("Not there yet..\n");
          sleep(1);
    
        }
    
       return 0;
    }

  9. #9
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    that is what mine resembles now, thanx for inputs.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  10. #10
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Last edited by c99; 03-09-2004 at 11:12 PM.
    R.I.P C89

  11. #11
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    thanx for the resource c99, but beg my pardon when I ask, what it has to do with this thread?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what it has to do with this thread?
    A creative way to increase one's post count perhaps? Not that I disapprove of course, that link can be useful. But it has no bearing on the topic of the thread that I can determine.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

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