abt using sleep() in windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayan4u
    New Member
    • Apr 2007
    • 86

    abt using sleep() in windows

    well i need to use a standard function for a time delay...i came across sleep() but it works n unix but m workin on windows usin dev c++...nd dev cant recogniz sleep() though i included unistd.h,dos.h, ....now i dnt want ne platform specific solution but a standard one....please help......
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    There is no standard portable sleep function, *nix has sleep Windows has Sleep.

    If you want a platform portable pause you will have to write a function to do it using standard library functions such as time(...)

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Banfa
      There is no standard portable sleep function, *nix has sleep Windows has Sleep.
      All unixes also have the Posix thread library (pthread) I don't know if there's
      a MS Windows implementation available (although MS Windows claims to be
      Posix compliant). The pthread library offers the pthread_cond_ti medwait function
      which can very well be used for an ordinary sleep() functionality.

      If you want a platform portable pause you will have to write a function to do it using standard library functions such as time(...)
      You're not suggesting a 'busy wait' are you? -)

      kind regards,

      Jos

      Comment

      • ayan4u
        New Member
        • Apr 2007
        • 86

        #4
        but if i use custom made sleep using time() i will b wasting valuable cpu cylcles....and its behaviour will be too unpredictable.. ..

        Comment

        • ayan4u
          New Member
          • Apr 2007
          • 86

          #5
          one more thn....i tried to use delay() by includin dos.h.........b ut it didnt work on mingw usin devcpp....it cant recognize the function even...thnk mingw ddnt support it...!!! ne idea...

          Comment

          • nmadct
            Recognized Expert New Member
            • Jan 2007
            • 83

            #6
            Originally posted by JosAH
            All unixes also have the Posix thread library (pthread) I don't know if there's
            a MS Windows implementation available (although MS Windows claims to be
            Posix compliant). The pthread library offers the pthread_cond_ti medwait function
            which can very well be used for an ordinary sleep() functionality.
            There is a native Win32 pthreads implementation:
            http://sourceware.org/pthreads-win32/

            There are also various other cross-platform libraries that can accomplish what you're looking for. For example, wxWidgets has a function called wxMilliSleep that does what you need.

            MS Windows does not exactly claim to be Posix-compliant. Windows NT was compliant with an earlier, less extensive Posix specification. I don't know if the same applies to later versions, and no version has ever claimed to be compliant with the more recent and comprehensive Posix standards.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by nmadct
              There is a native Win32 pthreads implementation:
              http://sourceware.org/pthreads-win32/

              There are also various other cross-platform libraries that can accomplish what you're looking for. For example, wxWidgets has a function called wxMilliSleep that does what you need.

              MS Windows does not exactly claim to be Posix-compliant. Windows NT was compliant with an earlier, less extensive Posix specification. I don't know if the same applies to later versions, and no version has ever claimed to be compliant with the more recent and comprehensive Posix standards.
              Ah, ok, so I guess all bets are off then when it comes to any current MS Windows
              compliancy w.r.t. any of the latest Posix Standards?

              What a mess this world is ...;-)

              kind regards,

              Jos

              Comment

              • nmadct
                Recognized Expert New Member
                • Jan 2007
                • 83

                #8
                Well, according to Wikipedia, the NT-based versions of Windows are Posix-compliant as long as you install Microsoft Windows Services for UNIX. I don't know how well that works.

                Comment

                • nmadct
                  Recognized Expert New Member
                  • Jan 2007
                  • 83

                  #9
                  UNIX-type operating systems support a lot more types of compatibility than just Posix, of course. To get an idea of the significance of this, consider that (again according the Wikipedia), while XP with MSW Services for UNIX is fully Posix-compliant, the list of those that are not officially compliant includes FreeBSD, NetBSD, OpenBSD and most Linux distros. Also, just because MS supports Posix interfaces doesn't mean it has quality implementations .

                  Comment

                  • luismy
                    New Member
                    • Nov 2008
                    • 1

                    #10
                    what about using:

                    #ifdef WIN32
                    #include <windows.h> // needed for Sleep
                    #else
                    #include <unistd.h>
                    #define Sleep(x) usleep((x)*1000 )
                    #endif

                    Comment

                    Working...