Thread: Best way to break out of loop

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Best way to break out of loop

    What would be the best way to break out of a loop in a timely manner, after a few secs for example. ( I dont need exactitude.)
    I know about the GetTickCount() method but i think it would be least CPU intensive to just increment and check against an integer.

    Is there a better way?

    Code:
    size_t i=0;
    
    while (1)
    {
                i++;
                // Do something again and again...
    
                // ... and now break out.
                if (i > 99)
                {
                    return;
                }
     }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i wouldnt think the integer method is advisable as you will get totally different results depending on processor speed, what is it you are aiming to do with this?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Yes i would get totally different results depending on processor speeds but i think with the speeds of today's processors it would be a few seconds more or less and as i said im not looking for precise timing.

    Its for this function that shakes the window for a few seconds in a game program that im writing.
    Last edited by Ducky; 09-04-2010 at 11:37 AM.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sleep Win API.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    No, you cant use Sleep because you're in an endless loop and you dont want to stop you need to call your function again and again.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So use threads?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    I tried threads too but apparently you cant close your thread as long as its running.
    I closed the handle but the thread kept on running.
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    reconsider something, you dont need a seperate function whileloop here, you main event/game loop calls the animation effect when required and local static count variable sets when animation ends, u can have a delay call of some kind in the function, but just do a frame of the animation on each call
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to signal a thread to end.
    Say if you want to execute an animation that will sleep n seconds and you will call this function x times within that period of n seconds.
    Then your thread should simply do whatever it needs to do, sleep n seconds, do something else it needs to do, then quit.
    You can then keep spawning as many threads as you need, how often you want, and know that the threads will exit when they're finished.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    @rogster
    Well thats still sounds to me more checking than comparing two numbers, though i dont really understand what you're proposing.
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    sorry its difficult to reply with code at moment, your for example your animate function is triggered, local count is limited to n frames, while not n, blit an animation frame, delay n milliseconds, exit function, game loop again calls animate, etc,
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    @rogster
    Thanks for your help i will try to understand it though im afraid im not on that level yet.

    @Elysia
    Thank you, im a looking into this signal function.
    Using Windows 10 with Code Blocks and MingW.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no thread signal function. What I mean when I write signal is that you need to incorporate your own logic to tell the thread that you want it to quit. This may be with a atomic variable, or an event (SetEvent/ResetEvent), etc.
    But the best thing would simply be to avoid the whole need for signaling in the first place. Ideally you would launch a thread, the thread does its thing and ends. Signaling adds complexity.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    sorry, my post is all messed up so no surprise hard to understand... game loop calls animate() if not maxcount show a frame of animation, call delay if preferred, increment local static count, function exit, back in gameloop, call animate, etc etc

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    68

    use sleep

    This problem is solved by the use of sleep as suggested above. Sleep will allow the OS to schedule other things to run on the thread that your loop is running.

    Anyway, you should NEVER NEVER NEVER EVER attempt to run a loop x amount of times for the purpose you suggested. There just isnt a good reason to do this. Use sleep() instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  2. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM