PDA

View Full Version : Ubuntu 9.04 G++ bug!



OVERPOWER8
December 16th, 2009, 09:35 PM
Hello.

The function sleep doesn't work right, how to fix it?

for example:


#include <iostream>
#include <unistd.h>

int main()
{
cout << "Hello world!";
sleep(10);
return 0;
}I don't know how does this happen, but before typing "Hello World", the program just hangs up for 10 seconds! The same bug in every program where I use operator sleep(ms). sleep(ms) is NOT! in the beginning, but it acts like it is in the beginning and *1000 (ms => s).

Can someone explain, why is this happening, and how can I fix it?
Thanks.

ajgreeny
December 16th, 2009, 10:53 PM
What function do you mean, suspend? Or are you speaking of the
sleep ##; some-commandwhich delays the running of the command by ## time?

OVERPOWER8
December 16th, 2009, 10:59 PM
What function do you mean, suspend? Or are you speaking of the
sleep ##; some-commandwhich delays the running of the command by ## time?

Yes, exactly.

I just need an extreme correct time delay, just like sleep(ms), but for some reason because of the sleep function, the program hangs.

ajgreeny
December 16th, 2009, 11:37 PM
What exactly are you trying to do, and what command, including the sleep part, are you using?

MadCow108
December 16th, 2009, 11:38 PM
its no bug its a feature

of course it "hangs" (its sleeping actually, so no cpu use)
you haven't flushed the output buffer

put a << endl behind the hello world which adds a endline character and flushes the stream
or just call cout.flush()

Logan513
December 16th, 2009, 11:43 PM
Hello.

The function sleep doesn't work right, how to fix it?

for example:


#include <iostream>
#include <unistd.h>

int main()
{
cout << "Hello world!";
sleep(10);
return 0;
}I don't know how does this happen, but before typing "Hello World", the program just hangs up for 10 seconds! The same bug in every program where I use operator sleep(ms). sleep(ms) is NOT! in the beginning, but it acts like it is in the beginning and *1000 (ms => s).

Can someone explain, why is this happening, and how can I fix it?
Thanks.

I think you need to read up on sleep. If I'm correct it's not sleep(ms); as it is sleep(s); So It IS doing it correctly...

Physical Hook
December 16th, 2009, 11:58 PM
sleep(seconds) - of course it'll hang for a while.
The easiest way to check whether it really hangs or simply does the job, add another cout right before the return statement.

OVERPOWER8
December 17th, 2009, 05:32 PM
its no bug its a feature

of course it "hangs" (its sleeping actually, so no cpu use)
you haven't flushed the output buffer

put a << endl behind the hello world which adds a endline character and flushes the stream
or just call cout.flush()

Thanks for explanation, now it works.

Zugzwang
December 17th, 2009, 05:42 PM
Anyway it hangs at the beginning. Is there a way to make it not to hang?

That's a little bit hard to believe. Can you post a *complete* program for us to verify that on our computers that actually compiles?

OVERPOWER8
December 17th, 2009, 06:20 PM
That's a little bit hard to believe. Can you post a *complete* program for us to verify that on our computers that actually compiles?

I made a mistake, it works.