Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference among sleep() , usleep() & [NSThread sleepForTimeInterval:]?

Can any body explain me what is the difference among sleep() , usleep() & [NSThread sleepForTimeInterval:] ?

What is the best condition to use these methods ?

like image 700
raaz Avatar asked Jul 01 '10 06:07

raaz


People also ask

What is the difference between Usleep and sleep?

The difference between sleep() and usleep() is that sleep() takes a number of seconds as its parameter, whereas usleep() takes a number of microseconds - millionths of a second - as its parameter.

What is the use of Usleep?

The function usleep() is a C API that suspends the current process for the number of microseconds passed to it. It can be used for delaying a job. DLYJOB works well if you are looking to delay a job for more than a second. If you need to delay the job for less than a second, however, you must use the usleep() API.

What is the use of sleep in PHP?

The sleep() function delays execution of the current script for a specified number of seconds. Note: This function throws an error if the specified number of seconds is negative.

How do I pause a PHP script?

The sleep() function in PHP is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds. The sleep( ) function accepts seconds as a parameter and returns TRUE on success or FALSE on failure.


2 Answers

sleep(3) is a posix standard library method that attempts to suspend the calling thread for the amount of time specified in seconds. usleep(3) does the same, except it takes a time in microseconds instead. Both are actually implemented with the nanosleep(2) system call.

The last method does the same thing except that it is part of the Foundation framework rather than being a C library call. It takes an NSTimeInterval that represents the amount of time to be slept as a double indicating seconds and fractions of a second.

For all intents and purposes, they all do functionally the same thing, i.e., attempt to suspend the calling thread for some specified amount of time.

like image 82
Jason Coco Avatar answered Sep 25 '22 01:09

Jason Coco


What is the best condition to use these methods ?

Never

Or, really, pretty much almost assuredly never ever outside of the most unique of circumstances.

What are you trying to do?

like image 22
bbum Avatar answered Sep 23 '22 01:09

bbum