   A Portable Delay Call??
  I'm creating a class.  In it, I want to use a delay function.  However, I want the class to remain portable between UNIX and Windows.  Obviously, I can't use MFC's Sleep.  I tried UNIX's sleep.  However, Windows couldn't find the header file that the call needs.  Any suggestions?   
  There really is no portable sleep function as far as I'm aware. You could always use some platform-independent OS library, but in essence even such libraries will only wrap a bunch of #ifdefs around a platform-specific call.void mySleep( unsigned int msecs ){#ifdef WIN32    Sleep( static_cast < DWORD > ( msecs ) );#else    // assume Linux / UNIX / whatever    sleep( msecs );#endif};  Allow the user to inject a sleep functor / function pointer maybe?   ;  clock() is standard C, and can be used in a loop. It won't relieve the CPU however, like non-standard Sleep functions would.   ;  Do you want this function to eventually get called asynchronously (in another thread), or be called synchronously in some future iteration of the game-loop/event-loop?If you go with the game-loop/event-loop option, you could package up your function and arguments using boost::bind or something similar and put it in a queue, which the loop body could check periodically.If you take the threaded approach, you could bundle up a functor in the same way and then use something like the following:// untestednamespace implementation{class delayed_call_impl{    public:   delayed_call_impl(const boost::xtime &delay, const boost::function<void ()> &f) :  delay_(delay),  f_(f)   {   }   void operator() () const   {  boost::xtime now;  boost::xtime_get(&now, boost::TIME_UTC);  delay_.sec += now.sec;  delay_.nsec += now.nsec;  boost::thread::sleep(delay_);  f_();   }    private:   boost::xtime delay_;   boost::function<void ()> f_;};} // close namespace implementationvoid delayed_call(const boost::xtime &delay, const boost::function<void ()> &f){    implementation::delayed_call_impl dc(delay, f);    boost::thread(dc);}// usagevoid f(int x, double y);// call f with args 1 and 2.0 after some_delaydelayed_call(some_delay, boost::bind(f, 1, 2.0));EDIT: Or if you'll forgive the shameless plug, you can use my asynchronous function call library and define your own invoker. This way you can call functions with return values and you won't have to worry about all the binding, which is done for you. You'll be set within 10 minutes ;)Edd[Edited by - the_edd on August 30, 2007 12:31:52 PM];Quote:Original post by Red AntThere really is no portable sleep function as far as I'm aware. You could always use some platform-independent OS library, but in essence even such libraries will only wrap a bunch of #ifdefs around a platform-specific call.*** Source Snippet Removed ***Just to clarify for the OP, POSIX sleep(x) delays x seconds, Win32 Sleep(x) delays x milliseconds, and POSIX usleep(x) delays x microseconds. As such, that example should probably call usleep(msecs*1000), instead of sleep(msecs).   ;  Thanks for all the help.  I think doing #ifdef or clock will be the quickest solution.   ;  Ah okay, thanks. :)   