 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gary Wessle Guest
|
Posted: Wed Sep 13, 2006 9:10 am Post subject: sleeping for less than a second |
|
|
Hi
who can I sleep(less than a second with out eating up the cpu).
the code below works for seconds but not fraction of, but will eat the
cpu as I learned googling.
further, man sleep on linux does not take fraction of a second.
thanks
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC;
while (clock() < endwait) {}
}
int main(){
// will eat up the cpu for nothing
int n;
cout << "Starting countdown...\n";
for (n=10; n>0; n--)
{
cout << n << endl;
wait (1);
}
cout << "left off" << '\n';
} |
|
| Back to top |
|
 |
Thomas Matthews Guest
|
|
| Back to top |
|
 |
m_schellens@hotmail.com Guest
|
Posted: Wed Sep 13, 2006 9:10 am Post subject: Re: sleeping for less than a second |
|
|
Gary Wessle wrote:
| Quote: | Hi
who can I sleep(less than a second with out eating up the cpu).
the code below works for seconds but not fraction of, but will eat the
cpu as I learned googling.
further, man sleep on linux does not take fraction of a second.
thanks
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC;
while (clock() < endwait) {}
}
int main(){
// will eat up the cpu for nothing
int n;
cout << "Starting countdown...\n";
for (n=10; n>0; n--)
{
cout << n << endl;
wait (1);
}
cout << "left off" << '\n';
}
|
Under linux or OS X, you can use nanosleep(...)
Cheers
Marc |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|