newbie: timers..

Discussion in 'C Programming' started by mike79, Nov 8, 2003.

  1. mike79

    mike79 Guest

    hi all,

    i am implementing a program using threads. I spawn a separate thread
    to execute a task every say 10 seconds. How I achieve this 10 second
    delay is by using a for/while loop, and incrementing the counter to a
    particular value relevant to 10 seconds. As follows:

    timerThread()
    {
    for (i = 0; i < 100000000; i++);
    // Next, do the task
    }

    As this is very resource/processor consuming (spending alot of time
    doing nothing), I was wondering if C provided any functions which are
    more efficient than this method.

    All help or suggestions are welcome, thank you for your help!
    mike79
     
    mike79, Nov 8, 2003
    #1
    1. Advertising

  2. (mike79) writes:
    > i am implementing a program using threads. I spawn a separate thread
    > to execute a task every say 10 seconds. How I achieve this 10 second
    > delay is by using a for/while loop, and incrementing the counter to a
    > particular value relevant to 10 seconds. As follows:
    >
    > timerThread()
    > {
    > for (i = 0; i < 100000000; i++);
    > // Next, do the task
    > }
    >
    > As this is very resource/processor consuming (spending alot of time
    > doing nothing), I was wondering if C provided any functions which are
    > more efficient than this method.


    It's also going to behave very differently when you run it on a slower
    or faster system. For that matter, any decent optimizing compiler can
    replace your loop with a single assignment: "i = 100000000;".

    There's no portable method in standard C to sleep for 10 seconds
    without gobbling CPU time. If you're using threads, you're already
    beyond the realm of portable C, so you might as well look into
    OS-specific solutions.

    <OFFTOPIC>
    Many systems provide a sleep() function that's probably just what
    you're looking for. If you need better than one-second resolution,
    there are other less portable solutions. If you have more questions
    about sleep(), or about threads, you should ask in a newsgroup devoted
    to whatever system you're using (after first reading your system's
    documentation, of course).
    </OFFTOPIC>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    (Note new e-mail address)
     
    Keith Thompson, Nov 8, 2003
    #2
    1. Advertising

  3. This is operating system dependent. You could try sleep for 10 seconds
    instead of looping.

    Another option would be to raise an event and catch the corresponding
    signal using a signal handler (works in Linux and Unix environment).

    What operating system are you using?

    Sandeep
    --
    http://www.EventHelix.com/EventStudio
    EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
     
    EventHelix.com, Nov 9, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Inkpen

    Timers in application web programming

    Stephen Inkpen, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    349
    Ken Cox [Microsoft MVP]
    Jul 16, 2003
  2. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    746
    Kelsang Wangchuk
    Jul 31, 2003
  3. Girish Pal Singh

    timers

    Girish Pal Singh, Aug 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    568
    Kevin Spencer
    Aug 11, 2003
  4. Dave Hagerich

    newbie quest about System.Timers.Timer

    Dave Hagerich, Dec 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    760
    S. Justin Gengo
    Dec 12, 2005
  5. Starwynd
    Replies:
    1
    Views:
    409
    Tor Iver Wilhelmsen
    Nov 23, 2003
Loading...

Share This Page