Professional Web Applications Themes

Sleeping in OS X / relinquishing processor time - Mac Programming

I have some code that needs to run about 30 times per second in order to work reliably (it is polling hardware). What is the best way to do this so the system doesn't take a big hit? Right now this little application is using up 90% of the processing time of my G4 1.25GHz, which is not ideal. It seems like the sleep() command only takes an integer command (I tried sleep(0.01) but it just slept and never woke up. I guess another option would be to make some kind of TimerTask. Would this not suffer from the same ...

  1. #1

    Default Sleeping in OS X / relinquishing processor time

    I have some code that needs to run about 30 times per second in order to
    work reliably (it is polling hardware).

    What is the best way to do this so the system doesn't take a big hit? Right
    now this little application is using up 90% of the processing time of my G4
    1.25GHz, which is not ideal. It seems like the sleep() command only takes
    an integer command (I tried sleep(0.01) but it just slept and never woke up.

    I guess another option would be to make some kind of TimerTask. Would this
    not suffer from the same problems? What's the best way to do this, and
    still guaranteeing that my code will be executed on a regular basis?

    Thanks,

    Alex

    Alex Guest

  2. #2

    Default Re: Sleeping in OS X / relinquishing processor time

    In article <BBC326C8.29E92%rampellsoft.com>,
    Alex Rampell <rampellsoft.com> wrote:
     

    In the System framework on OS X are:

    usleep - suspend process execution for N microseconds
    nanosleep - suspend process execution for N nanoseconds
    select - wait on an array of file refs, with timeout.

    In CarbonLib and Classic is TimeManager. For a routine that will be
    called at regular intervals, use InstallXTimeTask (the new version of
    InsXTime)
    <http://developer.apple.com/doentation/mac/Processes/Processes-67.html 

    #include <Timer.h>

    Remember that TimeManager callbacks run at interrupt time, so follow the
    rules: you can't do anything that would shuffle the heap. You can do
    most PBAsync I/O calls, using previously allocated parameter blocks.
    David Guest

  3. #3

    Default Re: Sleeping in OS X / relinquishing processor time

    Alex Rampell wrote: 

    Here's one approach:

    interface MyPoller : NSObject
    {
    NSTimer *pollTimer;
    }

    -(void) startPolling;
    -(void) quitPolling;
    - (void) pollOnce:(NSTimer *) theTimer;

    end

    implementation MyPoller

    - (void) startPolling
    {
    [self quitPolling]; // make sure you don't have any stale timers..
    pollTimer =
    [NSTimer scheduledTimerWithTimeInterval:0.03
    target:self
    selector:selector(pollOnce:)
    userInfo:nil
    repeats:YES];
    }

    -(void) pollOnce:(NSTimer *) theTimer
    {
    // whatever..
    }

    - (void) quitPolling
    {
    if (pollTimer)
    [pollTimer invalidate];
    pollTimer = nil;
    }

    end
    John Guest

  4. #4

    Default Re: Sleeping in OS X / relinquishing processor time

    In OSX, is there a preferred way to do a timer task? I'm looking to avoid
    Cocoa (just because I don't know Objective C)

    -Alex


    in article sf.sbcglobal.net, David Phillip
    Oster at org wrote on 10/28/03 12:20 AM:
     
    >
    > In the System framework on OS X are:
    >
    > usleep - suspend process execution for N microseconds
    > nanosleep - suspend process execution for N nanoseconds
    > select - wait on an array of file refs, with timeout.
    >
    > In CarbonLib and Classic is TimeManager. For a routine that will be
    > called at regular intervals, use InstallXTimeTask (the new version of
    > InsXTime)
    > <http://developer.apple.com/doentation/mac/Processes/Processes-67.html 
    >
    > #include <Timer.h>
    >
    > Remember that TimeManager callbacks run at interrupt time, so follow the
    > rules: you can't do anything that would shuffle the heap. You can do
    > most PBAsync I/O calls, using previously allocated parameter blocks.[/ref]


    Alex Guest

  5. #5

    Default Re: Sleeping in OS X / relinquishing processor time

    In article <BBC4514E.29F74%rampellsoft.com>,
    Alex Rampell <rampellsoft.com> wrote:
     

    There is a Carbon Event that works like a timer, but the name escapes
    me. I'm sure you could find it in the headers.
    Sean Guest

  6. #6

    Default Re: Sleeping in OS X / relinquishing processor time

    In article <aei.ca>,
    Sean McBride <org> wrote:
     
    >
    > There is a Carbon Event that works like a timer, but the name escapes
    > me. I'm sure you could find it in the headers.[/ref]

    You're thinking of Carbon event timers. :) See
    <http://developer.apple.com/doentation/Carbon/Reference/Carbon_Event_M
    anager_Ref/CarbonEventsRef/function_group_2.html>.

    -Eric

    --
    Eric Albert edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  7. #7

    Default Re: Sleeping in OS X / relinquishing processor time

    In article <BBC4514E.29F74%rampellsoft.com>,
    Alex Rampell <rampellsoft.com> wrote:
     

    All the options presented so far were Carbon-specific. Cocoa has
    NSTimer, NSThread and the likes.
    Uli Guest

  8. #8

    Default Re: Sleeping in OS X / relinquishing processor time

    In <idiom.com> John C. Randolph wrote:
    [snip] 
    My post is a bit late, but if you see this thanks a bunch - this really
    helped me. I didn't know about the nil assignment - it was just what I
    needed.

    ptk
    ptk Guest

Similar Threads

  1. Sleeping HP 2110 server on 5.06
    By Andrew Barnett in forum SCO
    Replies: 0
    Last Post: July 30th, 10:03 AM
  2. asp.net mysql sleeping processes
    By jan nielsen in forum ASP.NET General
    Replies: 0
    Last Post: July 23rd, 12:40 PM
  3. Replies: 0
    Last Post: July 22nd, 02:45 PM
  4. Replies: 10
    Last Post: July 21st, 12:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139