Click to See Complete Forum and Search --> : Inserting a delay ...


kaftab
June 26th, 2002, 04:08 PM
I have the following code for inserting a delay in the program:


#include <time.h>
void CreateHPGL::sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() );
}


and I use it like this:

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

PaulWendt
June 26th, 2002, 04:29 PM
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

Paul McKenzie
June 26th, 2002, 04:33 PM
Kamran,

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

Regards,

Paul McKenzie

JMS
June 26th, 2002, 04:36 PM
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.

Bob Davis
June 27th, 2002, 07:58 AM
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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/synchro_04tv.asp). 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!

kaftab
June 27th, 2002, 08:16 AM
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

Alexey B
June 27th, 2002, 09:44 AM
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.

JMS
June 27th, 2002, 01:13 PM
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....

kaftab
June 27th, 2002, 01:36 PM
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

cup
June 28th, 2002, 03:05 AM
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.

Paul McKenzie
July 3rd, 2002, 01:58 PM
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

Bob Davis
July 3rd, 2002, 04:36 PM
Pardon my ignorance, but what does a code profiler do?

cup
July 3rd, 2002, 04:46 PM
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.

Paul McKenzie
July 3rd, 2002, 05:08 PM
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