sleeping for less than a second



 DEVELOP > c-Plus-Plus > sleeping for less than a second

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER