• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: sleep() vs sleepUntilDate
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sleep() vs sleepUntilDate


  • Subject: Re: sleep() vs sleepUntilDate
  • From: Peter Sichel <email@hidden>
  • Date: Mon, 23 Sep 2002 09:19:58 -0400

At 12:31 AM -0400 9/23/02, Dylan Neild wrote:
Hi Everyone,

I was wondering what the effective, practical differences were between using the following calls:

sleep() - of course, part of the BSD environment.

-and-

(void)sleepUntilDate:(NSDate*)aDate - class method in NSThread.

Is there any major caveats to one or the other, besides the obvious argument differences. Effectively, what I'm wondering, is that if I use "sleep(10)" in a thread vs. "sleepUntilDate:(NSDate *)tenSecondsAway" (or vice versa) ... is there anything I should be aware of?

Many thanks,

Dylan

Since no one more knowledgeable has jumped in, I'll risk a collision
course with learning by sharing my limited understanding in public.

sleep() -

Is almost useless to Cocoa developers. It sleeps your process
to allow other BSD commands to execute. It's granularity is limited
to seconds.

(void)sleepUntilDate:(NSDate*)aDate - class method in NSThread.

Is integrated with the Run Loop mechanism so your UI and other threads
can continue to process events. It's granularity is milliseconds or
better.

To pause a thread for 200 ms for example, you could use:

[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:(NSTimeIntervale)0.2]];

The real power though is in re-designing your application to take
advantage of the run loop model. In my ping tool for example,
I need to check for time outs, and send additional pings periodically.
Instead of polling, I compute when the next send time or time out is due
and tell my thread to sleep until that time. A separate thread
just waits for receive data and the main thread continues to process
UI events.

The idea is to have your threads only wake up when there is
something useful to do.

Enjoy!

- Peter
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: sleep() vs sleepUntilDate
      • From: Lance Bland <email@hidden>
References: 
 >sleep() vs sleepUntilDate (From: Dylan Neild <email@hidden>)

  • Prev by Date: Re: Problem killing my threads :)
  • Next by Date: Re: Licences 101 - Copy Protection for Newbies
  • Previous by thread: sleep() vs sleepUntilDate
  • Next by thread: Re: sleep() vs sleepUntilDate
  • Index(es):
    • Date
    • Thread