Skip to content
Advertisement

In PHP, does sleep(0) still yield or is it ignored?

In PHP, if you use sleep(0);, will it be ignored (either by PHP or by the OS), or will it still yield execution to another thread temporarily? Does it incur any overhead? Does it depend on the version of PHP or the OS?

Advertisement

Answer

I decided to look into this again since someone upvoted the question.

As seen in basic_functions.c, it calls a function named php_sleep:

JavaScript

php_sleep is defined in php.h as follows (unless PHP is running on win32):

JavaScript

So it’s using the standard sleep which is being included with unistd.h elsewhere in the file, whose implementation probably depends on which platform you’re using and which flavor of libc and all that. I’ve seen posts on the internet claiming that old versions of the function would explicitly check if the argument was 0 and early exit, and others saying that sleep(0) is one way of cooperatively yielding your thread to the OS for the shortest time possible.

So after all this time, unsatisfyingly, the answer is: it depends.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement