Time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rasputin

    #1

    Time

    Is there any way to wait for a particular amount
    of time without depending on machine cycles etc?
  • Thomas Matthews

    #2
    Re: Time

    Rasputin wrote:[color=blue]
    > Is there any way to wait for a particular amount
    > of time without depending on machine cycles etc?[/color]

    Yes, and no.
    Yes, you can get the time add to it, then wait in
    a loop until the time reaches the new value. Research
    the "clock" and "time" functions.

    If you want fine resolution, you will have to use
    an operating system function to retrieve the time.

    Many real-time operating systems or even multi-
    tasking operating systems have the ability to
    put your program to sleep (block it) until an
    event occurs, such as a timer tick or a duration
    expires. This allows other tasks in the system
    to execute while yours is sleeping versus your
    programming hogging up CPU cycles waiting for
    a period of time and not letting other tasks
    execute. (Some smarter operating systems may
    swap other higher priority tasks in and out
    of memory while yours is doing nothing).

    Search a newsgroup related to your operating
    system or platform for more information.

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Peter Pichler

      #3
      Re: Time

      "Rasputin" <someone@neverl and.net> wrote in message
      news:pan.2004.0 4.09.17.02.26.5 40419@neverland .net...[color=blue]
      > Is there any way to wait for a particular amount
      > of time without depending on machine cycles etc?[/color]

      In standard C? You can wait in a loop until time() returns a different
      value. That's about it. And you are not guaranteed how often that happens.
      For anything "better" (for any definition of the word), use some platform
      extensions <OT>such as sleep(), wait() etc</OT>.

      Peter


      Comment

      • Rasputin

        #4
        Re: Time

        On Fri, 09 Apr 2004 18:30:25 +0100, Peter Pichler wrote:
        [color=blue]
        > "Rasputin" <someone@neverl and.net> wrote in message
        > news:pan.2004.0 4.09.17.02.26.5 40419@neverland .net...[color=green]
        >> Is there any way to wait for a particular amount
        >> of time without depending on machine cycles etc?[/color]
        >
        > In standard C? You can wait in a loop until time() returns a different
        > value. That's about it. And you are not guaranteed how often that happens.
        > For anything "better" (for any definition of the word), use some platform
        > extensions <OT>such as sleep(), wait() etc</OT>.
        >
        > Peter[/color]

        Thanks I found sleep. Sorry I should've looked into it
        more i guess before posting here.

        Comment

        • Rasputin

          #5
          Re: Time

          This version consumes uber amount of cpu.
          unistd.h is much better.

          #include <stdio.h>
          #include <time.h>

          void sleep( time_t delay );

          int main()
          {
          while(1) {
          puts( "Delaying for 3 seconds." );
          sleep( 3 );
          puts( "Done!" );
          }

          return 0;
          }

          void sleep( time_t delay )
          {
          time_t t0, t1;
          time( &t0 );
          do
          {
          time( &t1 );
          }
          while (( t1 - t0 ) < delay );
          }

          Comment

          • Keith Thompson

            #6
            Re: Time

            Rasputin <someone@neverl and.net> writes:
            [...][color=blue]
            > void sleep( time_t delay )
            > {
            > time_t t0, t1;
            > time( &t0 );
            > do
            > {
            > time( &t1 );
            > }
            > while (( t1 - t0 ) < delay );
            > }[/color]

            Apart from, as you pointed out, unnecessarily consuming lots of CPU
            time, this is non-portable. It assumes that subtracting a time_t from
            a time_t yields a meaningful time_t result representing the number of
            seconds between the two times. In fact, the standard's only guarantee
            is that time_t is an arithmetic type capable of representing times;
            there's no guarantee that it's integer as opposed to floating-point,
            that it's signed as opposed to unsigned, that the granularity is one
            second, or even that times are represented monotonically. (As it
            happens, most of these assumptions happen to be true on most existing
            systems.) If you want to write completely portable code, you need to
            assume that time_t values are as opaque as pointers.

            A more portable version of the above function would use the difftime()
            function to compute the difference, and the delay parameter would be a
            double, not a time_t. I'm not going to post a corrected version
            because I'm afraid someone might actually try to use it, perhaps even
            on a system that I'm trying to use at the same time. 8-)}

            If you're not concerned about 100% portability, you might as well use
            sleep() or whatever equivalent your system provides.

            (It wouldn't have been unreasonable, in my opinion, for the sleep()
            function to have been included in the C standard, but it wasn't.)

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • Malcolm

              #7
              Re: Time


              "Keith Thompson" <kst-u@mib.org> wrote in message[color=blue]
              >
              > (It wouldn't have been unreasonable, in my opinion, for the sleep()
              > function to have been included in the C standard, but it wasn't.
              >[/color]
              You're right. On any sort of multi-user / mutli-threaded program busy idling
              is an unspeakable sin.


              Comment

              • Keith Thompson

                #8
                Re: Time

                "Malcolm" <malcolm@55bank .freeserve.co.u k> writes:[color=blue]
                > "Keith Thompson" <kst-u@mib.org> wrote in message[color=green]
                > > (It wouldn't have been unreasonable, in my opinion, for the sleep()
                > > function to have been included in the C standard, but it wasn't.
                > >[/color]
                > You're right. On any sort of multi-user / mutli-threaded program busy idling
                > is an unspeakable sin.[/color]

                On the other hand, any system that provides the extensions necessary
                to support multi-threading, or even just multiple users or processes,
                will certainly provide an appropriate sleep() function. There's
                little need for sleep() in portable code; non-portable code might as
                well use a non-portable sleep() function.

                The alternative would have been to include such support in the C
                standard, but that was almost certainly impractical.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                Schroedinger does Shakespeare: "To be *and* not to be"

                Comment

                • CBFalconer

                  #9
                  Re: Time

                  Keith Thompson wrote:[color=blue]
                  > "Malcolm" <malcolm@55bank .freeserve.co.u k> writes:[color=green]
                  >> "Keith Thompson" <kst-u@mib.org> wrote in message[/color]
                  >[color=green][color=darkred]
                  >>> (It wouldn't have been unreasonable, in my opinion, for the sleep()
                  >>> function to have been included in the C standard, but it wasn't.
                  >>>[/color]
                  >> You're right. On any sort of multi-user / mutli-threaded program
                  >> busy idling is an unspeakable sin.[/color]
                  >
                  > On the other hand, any system that provides the extensions necessary
                  > to support multi-threading, or even just multiple users or processes,
                  > will certainly provide an appropriate sleep() function. There's
                  > little need for sleep() in portable code; non-portable code might as
                  > well use a non-portable sleep() function.[/color]

                  I disagree. Many single thread programs need a delay mechanism,
                  for which 'sleep' is a fine substitute. Some example usages:
                  Timing out interactive queries, allowing 'action' time for dumb
                  peripherals. The time resolution is a QOI factor, but not
                  normally critical.

                  The things that work with the sleep will very often be
                  non-portable.

                  --
                  Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net> USE worldnet address!

                  Comment

                  Working...