Makeing a delay in program.

This is a discussion on Makeing a delay in program. within the C++ Programming forums, part of the General Programming Boards category; Hello, I would like to make a delay in a program but I want to use something OS and API ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    Makeing a delay in program.

    Hello,
    I would like to make a delay in a program but I want to use something OS and API independent - I mean, is there any function in standard library of C++ to make something like that ? Or if there's not how to implement something like that in a good way. I though about makeing a loop which doesn't do anything. Is it an apriopriate solution ?
    Best regards.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    15
    May I ask what sort of application this loop is for? I don't necessarily know how to make a delay OS/API independent other than a loop, but depending on the situation a simple loop may be appropriate assuming you aren't trying to multitask.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I would just do it OS specific since it's one line for Unix and one line for Windows. It's probably one line for any Mac API, but I have no idea on that.

    Write a function to pause. Then you could use preprocessor checks (ie. #ifdef) to see if the program is being compiled for *nix or Windows. If Windows, use Sleep(). If *nix, use sleep(). Note the difference in the case of the 's'. Also note that they receive the same number of arguments but one takes in seconds, the other takes in milliseconds.

    You could possibly achieve a delay with a loop, but you can't be sure how long it will take to complete, especially as processors and computers in general get faster and take less time to do empty loops. Make sure you understand that this method also takes up a relatively large amount of CPU power. If you're really, really decided that you must use an ugly loop, you could get a timestamp from a function in time.h (or ctime since this is C++), figure out how delayed you want it to be, and keep looping until the necessary time has elapsed. This is not recommended.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    147
    If you find that a resolution of 1 second for the *nix version of sleep isn't sufficient (say you want the independent feature of sleep to work more like the Windows based Sleep in ms ) - you can create your own sleep using an object you can wait on. I did this for my own cross platform work, and the results made the function identical in operation. Wait's in *nix have resolution smaller than sleep.

    Even if you're not making threaded applications yourself, I highly recommend against using 'counting loops' for delays. If you were writing for DOS, and the target CPU's were within 25% of the same level of performance, and the delay time were not long (a string of if's not likely to combine in practice) - you might get away with it.

    However, given the very wide range of CPU performance targets, the fact that most people use an OS that is likely running multiple applications means that a timing loop is - well "Impolite" to other tasks

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thanks for replies. I'm coding a program which simulates some kind of a process evaluation. And I would like to introduce "time" in this program, so it would give an illusion that some process really needs time to be done. And of course the amount of time would depend on the kind of every process. But MacGyver (thanks) gave me a good argument to abandon a loop idea - on different CPUs it would take different amount of time to get it done. But really, I can't use OS functions. I need some other solution.
    Best regards.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Unless we're missing it, there isn't any other solution.

    Can you explain why OS API functions are out of the question?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You could write some code that would use Sleep() if compiled under Windows, some DOS function (delay()?) for DOS, some UNIX function for UNIX, and failing everything, a loop which eats the CPU but at least keeps your program running at the right pace.
    Code:
    #ifdef WIN32
        #include <windows.h>
    #else
        #include <time.h>
    #endif
    
    void delay_seconds(int seconds) {
    #ifdef WIN32
        Sleep(seconds);
    #elif defined(unix)
        /* ... */
    #else
        time_t start, now;
    
        time(&start);
        do {
            time(&now);
        } while(difftime(start, now) / CLOCKS_PER_SEC <= seconds);
    #endif
    }
    Something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you could also use boost's thread::sleep function. It's pretty clunky, but at least it's cross platform.
    "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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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