Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 18, 2008, 06:11 AM   #1
Darkroom
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
sleep() VS. usleep() VS. [NSThread sleepForTimeInterval:]

i don't really understand the difference between these 3 functions... assuming i have a program running, with several timers going on in the background for different things, and i execute an IBAction which would execute some stuff and then pause for 2 seconds before redirecting to another method in the class... which of these 3 functions are better to use and why?

Code:
-(IBAction)start:(id)sender
{
//do something;
//do something else;

[NSThread sleepForTimeInterval:2]; //or
sleep(2); //or
usleep(2000);

//then redirect to another method within the class
}
i should probably mention that while "usleep" seems to be an actual function and compiles without error: usleep(<#useconds_t #>), it doesn't work for me at all...
Darkroom is offline   0 Reply With Quote
Old Jul 18, 2008, 09:23 AM   #2
kpua
macrumors 6502
 
Join Date: Jul 2006
They're essentially all wrappers around nanosleep() and just provide different conveniences.

I think useconds_t is just a typedef for unsigned long or unsigned long long, so passing other integer types won't cause errors.


Haha... sorry. I didn't see the actual question in there—it was too early in the morning. Do what Catfish Man said.

Last edited by kpua; Jul 18, 2008 at 03:30 PM.
kpua is offline   0 Reply With Quote
Old Jul 18, 2008, 10:51 AM   #3
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
None of the above. Sleeping the main thread will just lag your program. Instead, schedule an NSTimer to fire two seconds later with the work you need done.
Catfish_Man is offline   0 Reply With Quote
Old Jul 18, 2008, 12:15 PM   #4
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
Quote:
Originally Posted by Darkroom View Post
i should probably mention that while "usleep" seems to be an actual function and compiles without error: usleep(<#useconds_t #>), it doesn't work for me at all...
The parameter of usleep() is in microseconds. That's millionths of a second. So usleep(2000) will sleep for 2 milliseconds (two thousandths of a second)--too fast for a human to perceive. sleep(2) would be equivalent to usleep(2000000).
iSee is offline   0 Reply With Quote
Old Jul 19, 2008, 01:13 AM   #5
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
Quote:
Originally Posted by iSee View Post
The parameter of usleep() is in microseconds. That's millionths of a second. So usleep(2000) will sleep for 2 milliseconds (two thousandths of a second)--too fast for a human to perceive. sleep(2) would be equivalent to usleep(2000000).
ooohhhh! i though it was just my lefts and rights that i got mixed up, now i've got to deal with microseconds and milliseconds too ... but at least now i know why it wasn't working... thanks for that...
Darkroom is offline   0 Reply With Quote
Old Jul 20, 2008, 05:58 AM   #6
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
i had noticed that [NSThread sleepForTimeInterval:2]; actually pauses my application instead of just the class it's in, which i had assumed the later would be the case...

after playing around with this function, i had noticed that the spinning beachball would show up if i set it this function to around 5 seconds... strange? yes/no?

i've therefore decided to use [NSThread sleepForTimeInterval:0.5]; instead of using NSTimer - simply because NSTimer gave unexpected/unwanted results in certain situations.
Darkroom is offline   0 Reply With Quote
Old Jul 20, 2008, 08:40 AM   #7
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Catfish_Man warned about this behavior above. I would look into NSTimer as be suggested.

-Lee
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
After sleep under 10.9 MBA doesn't wake up - sleep failure code? ratboy90 MacBook Air 4 Nov 16, 2013 05:44 AM
Mac Mini weird restart from sleep/screen sleep Gary86 Mac mini 5 Jan 15, 2013 02:40 PM
MBP won't wake from sleep using apple Remote and display recover from sleep in clamsh aoaaron MacBook Pro 1 Jan 10, 2013 10:33 AM
Mac Pro Snow Leopard Display will Sleep but Computer will not Sleep benos Mac Pro 0 Oct 26, 2012 06:56 PM

Forum Jump

All times are GMT -5. The time now is 07:04 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC