Making my own Sleep() function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nazwa
    New Member
    • Jul 2007
    • 3

    Making my own Sleep() function

    Hello

    How can I make my own Sleep function, without using while() and comparing time passed?
    I tried to find something useful on google, but I wasn't able to find anything useful.

    ----
    Nazwa
  • Girish Kanakagiri
    New Member
    • May 2007
    • 93

    #2
    Originally posted by Nazwa
    Hello

    How can I make my own Sleep function, without using while() and comparing time passed?
    I tried to find something useful on google, but I wasn't able to find anything useful.

    ----
    Nazwa
    Just Call a delay function with a parameter.
    make the function recursive by decrementing the parameter.
    Give exit status as parameter value as Zero to come out.
    For Ex:
    Code:
    void delay(20); // function call
    
    //Delay Function
    void delay(int i)
    {
     if (i == 0)
     return;
     delay(i-10);  // give the decrement as 1 or 10 or 100 as convenient
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Please don't do this:
      Originally posted by Girish Kanakagiri
      //Delay Function
      void delay(int i)
      {
      if (i == 0)
      return;
      delay(i-10); // give the decrement as 1 or 10 or 100 as convenient
      }
      The delay() function recursively calls itself. This will lead to a crash when the stack memory is exhausted.

      With a 3.8GHZ processor, you will need a very big loop indeed to delay even a small amount.

      What is wrong with using the OS sleep function in the first place??

      Comment

      • tnga
        New Member
        • Jul 2007
        • 27

        #4
        Originally posted by Girish Kanakagiri
        Just Call a delay function with a parameter.
        make the function recursive by decrementing the parameter.
        Give exit status as parameter value as Zero to come out.
        For Ex:
        Code:
        void delay(20); // function call
        
        //Delay Function
        void delay(int i)
        {
         if (i == 0)
         return;
         delay(i-10);  // give the decrement as 1 or 10 or 100 as convenient
        }
        LOL, that's cool!!!!

        Comment

        • seforo
          New Member
          • Nov 2006
          • 60

          #5
          Why do you want to reinvent the wheel. Sleep is already there. You can simply do this
          [CODE=c]
          #include <unistd.h>
          int main()
          {
          sleep(4);
          return 0;
          }
          [/CODE]

          Comment

          • Nazwa
            New Member
            • Jul 2007
            • 3

            #6
            Problem with sleep is that It takes seconds as parameter, and I need miliseconds. I can always make my own function by creating while loop which runs for requested time, but it uses full CPU. I tried with unistd.h but it's not working under BCB and C::B.
            Do you have any other idea how to do it? I've seen something like that made in short asm code long, long time ago, but can't find it again.

            Comment

            • Girish Kanakagiri
              New Member
              • May 2007
              • 93

              #7
              Originally posted by Nazwa
              Problem with sleep is that It takes seconds as parameter, and I need miliseconds. I can always make my own function by creating while loop which runs for requested time, but it uses full CPU. I tried with unistd.h but it's not working under BCB and C::B.
              Do you have any other idea how to do it? I've seen something like that made in short asm code long, long time ago, but can't find it again.
              Code:
              //function call
              mysleep(50);
              
              //function
              void mysleep(int i)
              {
                for(int n=1;n<i;n++);
              }
              I think this is what you are looking for.

              Comment

              • JonLT
                New Member
                • Jul 2007
                • 41

                #8
                [code=cpp]
                #include <ctime>

                void mySleep(int sleepTime)
                {
                int curTime = clock(); //get the current time
                while(clock() - curTime < sleepTime){} //wait until the time has passed
                }
                [/code]

                the function takes milliseconds as argument. It works by first getting the current time, and the subtracting that from the real current time, and when the result of that is bigger than the passed value, the time has passed.

                Comment

                • Nazwa
                  New Member
                  • Jul 2007
                  • 3

                  #9
                  Originally posted by JonLT
                  [code=cpp]
                  #include <ctime>

                  void mySleep(int sleepTime)
                  {
                  int curTime = clock(); //get the current time
                  while(clock() - curTime < sleepTime){} //wait until the time has passed
                  }
                  [/code]

                  the function takes milliseconds as argument. It works by first getting the current time, and the subtracting that from the real current time, and when the result of that is bigger than the passed value, the time has passed.
                  It works, but it uses 100% CPU, and point is to make it to use much less.
                  After all, I think there is nothing more to deal with.

                  Thanks for help.

                  Comment

                  • RRick
                    Recognized Expert Contributor
                    • Feb 2007
                    • 463

                    #10
                    You're back to using a system call for your timeouts.

                    On Unix there's something called nanosleep. It gets around the "seconds" limitation of the sleep command. There's also "gettimeofd ay" which returns time with sub-second accuracy.

                    I'm not sure what Windows uses, but I'm pretty sure it has one, too.

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by RRick
                      You're back to using a system call for your timeouts.

                      On Unix there's something called nanosleep. It gets around the "seconds" limitation of the sleep command. There's also "gettimeofd ay" which returns time with sub-second accuracy.

                      I'm not sure what Windows uses, but I'm pretty sure it has one, too.
                      The Sleep function in Windows already takes milliseconds as it's parameter unit

                      Comment

                      • Akai
                        New Member
                        • Aug 2007
                        • 1

                        #12
                        Originally posted by Nazwa
                        It works, but it uses 100% CPU, and point is to make it to use much less.
                        After all, I think there is nothing more to deal with.

                        Thanks for help.

                        Hello!

                        I had the same problem using a function like that, but I think that I found a solution:


                        Code:
                        #include <time.h>
                        
                        void staticSleep(int frequency)
                        {
                            clock_t goal = ( frequency * 1000 ) + clock();
                            while (goal > clock())
                              Sleep( 1 );
                        }
                        Where "frequency" is the time in seconds that you want to wait.

                        I hope that it is what you looking for.

                        Comment

                        Working...