CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    May 2001
    Posts
    153

    Inserting a delay ...

    I have the following code for inserting a delay in the program:

    Code:
    #include <time.h>
    void CreateHPGL::sleep( clock_t wait )
    {
       clock_t goal;
       goal = wait + clock();
       while( goal > clock() );
    }
    and I use it like this:
    Code:
    sleep(20000); // for a 20 second delay
    It works fine on windows, however, on unix, there is no delay. Can someone point out the problem for me?

    Kamran
    Last edited by kaftab; June 26th, 2002 at 04:11 PM.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I always use the Sleep() function on windows [argument in milliseconds] and the sleep() function on unix systems [argument in seconds].

    If you're using C++, check out http://www.boost.org; they have a class hierarchy that takes delays into account [look into the thread class].

    --Paul

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449
    Kamran,

    Why not use the sleep() function that is already available on Unix?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    Really really bad idea.....

    Do you know what happens when you put your program into a tight loop like that on a 10 mip machine? Your CPU executes that loop like 20 million times in two seconds. It will crush your performance if you do it often or if your computer is working on something else. It is really really a bad idea and I'm not like some on this board who say that often. Generally I'm an if it works kind of guy, but even I wouldn't recoomend that solution.


    If you want to make a program sleep then use the sleep command!! In unix it's standard. Microsoft sleep sleeps for seconds. Unix sleep sleeps for miliseconds. or visa versa..

    One can also use

    DWORD SleepEx(
    DWORD dwMilliseconds, // time-out interval
    BOOL bAlertable // early completion option
    );

    On Microsoft NT..



    Semephores can also be employed with time outs to simulate light weight sleep commands if the real sleep command doesn't work for you. semephores aren't platform independent though.

  5. #5
    Join Date
    Jan 2001
    Posts
    588
    These guys have some good suggestions. I use Sleep() myself when I need a delay. If you need something a little more flexible, you may want to check out "waitable timers" in MSDN. Like JMS said, you can also use semaphores and things like that.

    Edit: I just now realized that you wanted it to work on both Windows and Unix. I guess this platform-dependent code won't be much of a help now, but maybe you can use it later or something. Oops!

  6. #6
    Join Date
    May 2001
    Posts
    153
    Thanks for all the replies!

    I did not find a sleep() or Sleep() function in C++. I assume you are talking about the system command "sleep [seconds]" here.

    Checked out boost.org, interesting stuff, however, I don't think I will use it, want to keep it simple. I have rogue wave tools library. That probably has a function too, however, want to stay away from such dependencies.

    JMS, I realize the "badness" of this function that I was trying to use. Point taken, I will instead use the system command "sleep". I had simply copied that function from somewhere (I think codeguru!) without thinking about what it was doing.

    So, in conclusion, I will use the system command "sleep", even though I would've preferred a C++ solution.

    Thanks again, everyone.

    Kamran

  7. #7
    Join Date
    May 2001
    Posts
    472
    The system is the only thing that can stop your program and wake it up again, unless you want to create your own threads. Why reinvent the wheel?

    By the way, clocks are too managed by the system. As has already being said, the way you throw your program into a loop is a terrible idea, since it won't let threads with the same or lower priority than yours run while your program sleeps.
    Last edited by Alexey B; June 27th, 2002 at 10:19 AM.
    Ce n'est que pour vous dire ce que je vous dis.

  8. #8
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    kaftab,

    Although the consensus is that the idea of putting a program into a tight loop to simulate a wait state is a bad idea, It's certainly inventive. My own reaction to this idea was so strong cause I've actually seen this technique before. I was working on the small parcel sorting system down in Memphis Tenn, at Fedex. This server code was collecting billing files and inserting them into a database collected from about 40 sco unix boxes which ran this huge Crisplant sorting system for flat packages.
    All visiblilty into the sort came from these files as well as the financial data. Anyway the machine they had was pretty beefy for the day. Quad processor should have been more than enough for the task. Running the sorters at about 25% capacity this machine was topped out. They upgraded to more expensive boxes ( server was redundant ) and still didn't work. Dude who wrote the program employed what he described as "virtual semephores". What is a virtual semephore you ask, it's an integer in a tight loop.

    Anyway the program worked and that's saying something. But replacing the "virtual semephores" with just plain old semephores allowed us to run on the original boxes and not saturate the cpu's.

    Just a story to explain my reaction, didn't mean to come down on you hard....

  9. #9
    Join Date
    May 2001
    Posts
    153
    JMS,

    Thaks for sharing the story. Gives a bit of context to this issue.

    And certainly I don't mind a bit of "scolding" from more experienced programmers, it's part of the learning process!

    Kamran

  10. #10
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    sleep does not work in a multithreaded situation if you are using Solaris. It is something to do with SIG_ALARM. However, all is not lost: you can always use nanosleep.
    Succinct is verbose for terse

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by JMS
    kaftab,

    Although the consensus is that the idea of putting a program into a tight loop to simulate a wait state is a bad idea, It's certainly inventive. My own reaction to this idea was so strong cause I've actually seen this technique before. I was working on the small parcel sorting system down in Memphis Tenn, at Fedex. This server code was collecting billing files and inserting them into a database collected from about 40 sco unix boxes which ran this huge Crisplant sorting system for flat packages.
    All visiblilty into the sort came from these files as well as the financial data. Anyway the machine they had was pretty beefy for the day. Quad processor should have been more than enough for the task. Running the sorters at about 25% capacity this machine was topped out. They upgraded to more expensive boxes ( server was redundant ) and still didn't work. Dude who wrote the program employed what he described as "virtual semephores". What is a virtual semephore you ask, it's an integer in a tight loop.

    Anyway the program worked and that's saying something. But replacing the "virtual semephores" with just plain old semephores allowed us to run on the original boxes and not saturate the cpu's.

    Just a story to explain my reaction, didn't mean to come down on you hard....
    Didn't FedEx ever hear of a piece of software called a "code profiler"?

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Jan 2001
    Posts
    588
    Pardon my ignorance, but what does a code profiler do?

  13. #13
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    A code profiler works out how much time is spent in each area of code.

    In Visual C++, it is one of the options under the Build menu.

    On Solaris, there is a compilation flag to switch on profiling. You then use gprof to look at the results.
    Succinct is verbose for terse

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Bob Davis
    Pardon my ignorance, but what does a code profiler do?
    Also (to add to cup's answer) there are some great third-party ones available (for the Windows platform).

    The unwritten rule is if you want to optimize your application, you use a profiler before changing code around, or worse, getting new hardware. There is a saying that "Wishful optimizing without profiling is the scourge of the programmer", or something like that.

    JMS's experience is just an example of that. They thought that changing the machines would improve performance, not knowing where the real bottleneck was. The same thing on the software end -- a lot of programmers who don't profile their code spend time optimizing code that has nothing or has minimal impact on the speed of the program. With the use of a profiler in both cases, time and money could have been saved instead of chasing wild geese.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured