C++ Learning Community Forum
March 17, 2010, 02:39:44 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: millisecond sleep function ?  (Read 3393 times)
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1225



View Profile
« on: June 01, 2008, 11:41:00 PM »

I'm wanting a cross platform function that will stop the calling thread's execution for a given amount of time. I know Windows has the Sleep() function and that Linux has the usleep() function. The problem comes when trying to make them both behave in similar ways- Sleep deals in milliseconds, usleep deals in microseconds. So that pretty much limits me to using a function with a minimum of a millisecond of sleep time . So what I'm wondering is, is there a function in Linux which allready has a millisecond resolution? or do i have to just do like
Code:
usleep(amount_of_milliseconds*1000);
Huh
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1225



View Profile
« Reply #1 on: June 03, 2008, 02:25:18 AM »

here is what i did:

http://steppingstone.svn.sourceforge.net/viewvc/steppingstone/src/sleep.cpp?revision=5&view=markup

Now about putting the headers in the namespace too- was that necessary or could i have just prefixed calls to the Sleep and usleep with :: and gotten away with it? remember unistd.h has a function called "sleep" in it, so i wasn't sure if that would screw up my class's name or what. Huh
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
JoeImp
N00b!!1
*
Posts: 19


View Profile
« Reply #2 on: June 15, 2008, 07:58:23 PM »

I use wxwidgets a lot which can be used cross platform and has a Sleep() function.

http://docs.wxwidgets.org/2.8.6/wx_wxthread.html#wxthreadsleep

Takes milliseconds as an argument.
Logged
oldneuro
N00b!!1
*
Posts: 17


View Profile
« Reply #3 on: December 12, 2008, 01:00:23 AM »

this is so easy

#ifdef _WIN32
#define SLEEP(x)   Sleep(x)
#else // _WIN32
#define SLEEP(x)   usleep(x*1000)
#endif // _WIN32
Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1225



View Profile
« Reply #4 on: December 12, 2008, 04:33:15 AM »

You could, but I have two problems for that-

1. no type checking
2. bounds checking is needed for usleep there
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
michaelp
baseball 4 life!
rand()%title;
**
Posts: 204



View Profile
« Reply #5 on: December 12, 2008, 10:06:11 PM »

http://www.cplusplus.com/reference/clibrary/ctime/clock.html
Logged


----------------------
mleveill
1337 L0LCoder
C++ Freak
***
Posts: 430


IM IN UR PROGRAMZ WRITIN UR CODEZ!


View Profile
« Reply #6 on: December 13, 2008, 06:42:41 AM »


probably unreliable if running on a multicore machine
Logged

Quote from: kohlrak
Common interests aren't always the best thing. Especially programming wise.
I could just picture a couple fighting over weather to "xor eax by eax" or "and eax by 0"...
Then kids'll come home and argue with their parents that readabilty is better than size and that "mov eax, 0" would be best because that's "more readable." (Even if it really isn't...)

LOLCode - http://lolcode.com/home
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1225



View Profile
« Reply #7 on: December 23, 2008, 06:58:31 AM »

michaelp - Also that won't do what i intend. I wanted to halt execution of my code for a short time, rather than just having my code run a loop. Nice try anyway though  Wink
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
oldneuro
N00b!!1
*
Posts: 17


View Profile
« Reply #8 on: December 23, 2008, 09:50:50 PM »

Do I really have to spell it out?  You want type and bounds checking?

#ifdef _WIN32
#define SLEEP(x)   Sleep(x)
#else // _WIN32
#define SLEEP(x)   usleep(x*1000)
#endif // _WIN32

void mysleep (unsigned x)
{
        if (x > UPPER_LIMIT) { error(); }
        SLEEP(x);
}
Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1225



View Profile
« Reply #9 on: December 23, 2008, 11:52:46 PM »

*shrug* to me, if i wrote that, it seems more like a hack I'd hope no other programmer ever touched because it seems like a violation of OO encapsulation principles. Its probably fine for C ( Huh ), it just seems very out of place as C++. I guess those are some more reasons I prefer my way Though I fully admit i could be wrong. What does everyone else think?
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
oldneuro
N00b!!1
*
Posts: 17


View Profile
« Reply #10 on: December 24, 2008, 05:54:58 AM »

right, in my mind, your answer and mine are the same.  The point is, you just make it use usleep(x*1000) on *nix and Sleep(x) on win32.  The rest is just filler, and important though it may be, the underlying solution is the same.  So, we agree.
Logged
Joel
Coding from an Igloo
Jr. Nerd
***
Posts: 47


Linux coder


View Profile WWW
« Reply #11 on: August 02, 2009, 03:01:58 PM »

I use wxwidgets a lot which can be used cross platform and has a Sleep() function.

http://docs.wxwidgets.org/2.8.6/wx_wxthread.html#wxthreadsleep

Takes milliseconds as an argument.
Also my #1 option in cross platform Smiley
Logged


* Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM
* Ubuntu 9.04; Kernel 2.6.28-15-generic.
* lighttpd, php5, perl, eruby, python.
* geany, HTML & CSS & JavaScript, mono, gtk+, QT4, wxWidgets, bash, gcj.
* Coming soon: Kubuntu 9.10 or Dragora 1.1
Neken
N00b!!1
*
Posts: 7



View Profile
« Reply #12 on: November 11, 2009, 05:21:59 AM »

yeah, this code should be

Code:
#include "sleep.h"

#if defined _WIN32
#include <windows.h>
#elif defined __linux__
#include <unistd.h>
#endif

void ih8sleep::sleep::milliseconds(uint32_t milsecs)
{
  #if defined _WIN32
  ::Sleep(milsecs);
  #elif defined __linux__
  ::usleep(milsecs*1000);
  #endif
}
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!