Best way to break out of loop

This is a discussion on Best way to break out of loop within the C++ Programming forums, part of the General Programming Boards category; It's Sleep (capital S)....

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    It's Sleep (capital S).
    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.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    @smasherprog
    As i stated above i tried to use Sleep with Createthread but i couldnt manage to close the thread (CloseHandle) once the time elapsed.
    Please advise if you got a solution for that problem, thanks.

    @rogster001
    Its a GUI not a console program so i dont have a game loop.
    Compiler MSVC++ 2013 with Code::Blocks.

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Ducky View Post
    @smasherprog
    As i stated above i tried to use Sleep with Createthread but i couldnt manage to close the thread (CloseHandle) once the time elapsed.
    Please advise if you got a solution for that problem, thanks.
    Yes but CloseHandle isn't meant to stop a thread. CloseHandle is simply your way of saying, I'm no longer interested in what happens with regards to that thread and no longer need any control over it. That's actually counter-productive if your goal is to stop it!

    There are several ways to actually make a thread exit when you want to, but fortunately you aren't paying much attention to some of the suggestions as to how that can be done. It's just as well because if you can do it without a thread then you're probably better off doing it without a thread, especially at your level of expertise.

    You actually had about the best solution already, before you came here. Use GetTickCount, possibly combined with Sleep if you would otherwise create a busy-loop.
    Last edited by iMalc; 09-05-2010 at 01:45 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Yes, thank you iMalc for the clarification.

    Now im looking into setting a flag on the thread and calling ExitThread.

    I would use GetTickCount if i was worried by punctuality but im not and comparing two integers must
    be less CPU stressing than calling a function every time.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Avoid using ExitThread. This usually indicates a bad design.
    Let the thread do its work and then terminate. Do not signal it to close.
    Design your function so that it does its task without needing to terminate! What is so difficult?
    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.

  6. #21
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    .

    i dont mean for console app, i use this method to animate pop up windows, it just means there is no local loop that takes over in an animation function and events can still be monitored, sleep is called just once in the function until that function call is skipped, ie the animation is complete
    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'"

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    @rogster
    I appreciate your help but without code i dont understand a word you're saying, i think im just not there yet.

    @Elysia "What is so difficult?"
    Its just me agonising over the GetTickCount() method thinking that calling a function every milliseconds would put unnecessary stress on the CPU. But im probably wrong.

    But its ok i found a method to close a thread from outside.
    WaitForSingleObject with CreateEvent.
    Compiler MSVC++ 2013 with Code::Blocks.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    What good will this do? If you need to spawn a new animation or something every millisecond, then using synchronization primitives such as WaitForSingleObject will only slow it down!
    I keep repeating this: the thread should do its thing and close. You should not have to tell the thread to close. If that is the case, then the code to manage whatever you are doing is in the wrong place. Don't put it inside the thread.

    Example: Animation.
    The animation consists of moving a pixel every 2 ms. Then the thread would move pixel, wait 2 ms, move pixels, etc, until the object has moved to the desired position. Then the thread exits.
    Now, the main thread, schedules these animations, spawning a thread for each animation, as many as it takes, how often it wants.
    No synchronization. No telling the thread to exit.

    Am I completely off what you want here?
    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.

  9. #24
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Well if i shouldnt close the thread from outside then i dont need thread at all because i have a while(1) loop in my function and either way i have to check at every loop if its time to break out or not.
    And for that the best and "only?" way would be GetTickCount().
    Compiler MSVC++ 2013 with Code::Blocks.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Ducky View Post
    Well if i shouldnt close the thread from outside then i dont need thread at all because i have a while(1) loop in my function and either way i have to check at every loop if its time to break out or not.
    And for that the best and "only?" way would be GetTickCount().
    GetTickCount is not slow. I've seen it successfully used in production code combined with Sleep and a critical-section, as part of a reader/writer lock. It works very well there.
    I hope you'll just jump in and use it finally.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #26
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    lol Ok you convinced me!
    Compiler MSVC++ 2013 with Code::Blocks.

  12. #27
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    I know you just said you've been convinced but for posterity's sake...

    GetTickCount() is a trivial call and it's very fast, but let's test it anyway to be sure. Let's time a few batches of GetTickCount() to see how long they take, how about 1 billion calls?
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        for (int i = 0; i < 5; ++i)
        {
            DWORD oldtick = GetTickCount();
            for (unsigned int ui = 0; ui < 1000000000; ++ui)
            {
                GetTickCount();
            }
            cout << "Took " << (GetTickCount() - oldtick) << "ms\n";
        }
    
        return 0;
    }
    Output on my system:
    Took 4196ms
    Took 4118ms
    Took 4134ms
    Took 4119ms
    Took 4134ms


    Maybe you think QueryPerformanceCounter() is faster?
    Code:
    #include <Windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        LARGE_INTEGER li;
        for (int i = 0; i < 5; ++i)
        {
            DWORD oldtick = GetTickCount();
            for (unsigned int ui = 0; ui < 1000000000; ++ui)
            {
                QueryPerformanceCounter(&li);
            }
            cout << "Took " << (GetTickCount() - oldtick) << "ms\n";
        }
    
        return 0;
    }
    Took 30108ms
    Took 30202ms
    Took 30155ms
    Took 30155ms
    Took 30139ms

    ... yeah.

  13. #28
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Thats pretty fast indeed but it makes your CPU works 100%. (50% if you got a dual core)

    Now put a Sleep(1); in the loop to avoid this and the performance is gonna drop down significantly.

    A 1000 tries take 2 secs.
    Compiler MSVC++ 2013 with Code::Blocks.

  14. #29
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Can we see the code you're trying to run in a loop?

  15. #30
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Ducky View Post
    Thats pretty fast indeed but it makes your CPU works 100%. (50% if you got a dual core)

    Now put a Sleep(1); in the loop to avoid this and the performance is gonna drop down significantly.

    A 1000 tries take 2 secs.
    Well when you're repeatedly telling it to sleep for 1ms, that's going to add up to a lot of time sleeping if you don't do much else. In fact the sleeping alone there has to account for at least 1 second according to how Sleep is documented, and typically more. The fact that your real code will actually be doing something useful will mean that overall it wont be spending quite as much time sleeping.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 3 FirstFirst 123 LastLast
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, 05:38 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 01:16 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 10:45 PM

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