ih8censorship
Megalomaniac!!!
Administrator
C++ guru
    
Posts: 1225
|
 |
« 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 usleep(amount_of_milliseconds*1000); 
|
|
|
|
|
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
|
 |
« 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=markupNow 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. 
|
|
|
|
|
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
|
 |
« 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
|
 |
« 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?
|
|
|
|
|
mleveill
1337 L0LCoder
C++ Freak
  
Posts: 430
IM IN UR PROGRAMZ WRITIN UR CODEZ!
|
 |
« Reply #6 on: December 13, 2008, 06:42:41 AM » |
|
probably unreliable if running on a multicore machine
|
|
|
|
|
Logged
|
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
|
 |
« 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 
|
|
|
|
|
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
|
 |
« 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
|
 |
« 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 (  ), 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
|
 |
« 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
|
 |
« Reply #11 on: August 02, 2009, 03:01:58 PM » |
|
Also my #1 option in cross platform 
|
|
|
|
|
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
|
 |
« Reply #12 on: November 11, 2009, 05:21:59 AM » |
|
yeah, this code should be #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
|
|
|
|
|