usleep is not working on Windows

Asked

Viewed 83 times

3

I’m using usleep to simulate slower internet in my projects, so as to have a better perception of the screens when there are loads for ajax requests for example.

Using a linux environment seems to work normal if use:

usleep(2500);

But in Windows seems to have no effect, the delay does not occur. I know that the usleep has some problems that can vary for each processor and I even know this alternative php.net#54572:

dl('php_w32api.dll');

$GLOBALS['win32api'] =& new win32;

// USleep alternative for Windows and PHP4:
$GLOBALS['win32api']->registerfunction("long Sleep (long dwMillisecods) From kernel32.dll");

// Now you can call the function from everywhere in your script: $GLOBALS['win32api']->Sleep(milliseconds);

for ($msec = 2000; $msec > 0; $msec = $msec - 125) {
  echo "Hi. Next one in $msec msec.\n";
  $GLOBALS['win32api']->Sleep($msec);
}

But where I read the problems are related the time differences and processor reaching 100%, but did not find on the function "not work".

I’m using:

  • Processor: Interl Core i5 m450 (2.40 Ghz)
  • PHP 5.4.12 (Thread Safety)
  • Architecture x64
  • Server API: Apache 2.0 Handler

Is it a variant processor or PHP version?

1 answer

2


According to the DOC, usleep - Delays execution by millionth of a second, soon, usleep(2500) can be imperceptible in the simulation test you are doing. Perhaps the accuracy is not 100% working with millionth of a second, but here usleep(2500) works properly.

echo microtime(1)
usleep(2500)
echo microtime(1)

1439005351.1457
1439005351.1487

The only 'bug' reference reported in the Windows environment I found was, Function usleep doesn’t work under Windows, and in my opinion, it deals with a misconception when using usleep (500) and expect a delay of 1/2 second.

  • It really was a gaffe of mine, usleep(2000000); should be right for 2 seconds for example.

  • @Guilhermenascimento, I thought you were doing some more detailed measurement. It would be a comment, but it did not fit and turned response. rs

  • 1

    in my millionth idea was for "thousand", but your comment is the right answer, it was a mistake of mine, since millionth refers to the one who occupies the position of million in a series. : ( sometimes I even cheat myself kkkkkkkk! Thank you very much. If you want to add the explanation about millionth to other "confused" people. Thank you. https://pt.wiktionary.org/wiki/milionésimo

  • 1

    @Guilhermenascimento, the comments complement the responses, the merit is his :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.