C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sleeping for less than a second

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gary Wessle
Guest





PostPosted: Wed Sep 13, 2006 9:10 am    Post subject: sleeping for less than a second Reply with 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';
}
Back to top
Thomas Matthews
Guest





PostPosted: Wed Sep 13, 2006 9:10 am    Post subject: Re: sleeping for less than a second Reply with quote



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

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
Back to top
m_schellens@hotmail.com
Guest





PostPosted: Wed Sep 13, 2006 9:10 am    Post subject: Re: sleeping for less than a second Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.