| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Gary Wessle" |
| Date: |
12 Sep 2006 11:15:51 PM |
| Object: |
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';
}
.
|
|
| User: "" |
|
| Title: Re: sleeping for less than a second |
13 Sep 2006 02:21:37 AM |
|
|
Gary Wessle wrote:
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
.
|
|
|
|
| User: "Thomas Matthews" |
|
| Title: Re: sleeping for less than a second |
12 Sep 2006 11:41:53 PM |
|
|
Gary Wessle wrote:
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
This is a FAQ, see my signature below.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
.
|
|
|
|

|
Related Articles |
|
|