Created
April 8, 2014 21:46
-
-
Save amcsi/10197791 to your computer and use it in GitHub Desktop.
Pthreads sleep() experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyThread extends Thread | |
{ | |
public function run() | |
{ | |
sleep(1); | |
echo "ran\n"; | |
} | |
} | |
$start = microtime(true); | |
for ($i = 0; $i < 5; $i++) { | |
echo "before start\n"; | |
$obj = new MyThread; | |
$obj->start(); | |
echo "after start\n"; | |
} | |
printf("Run time: ~%ds\n", round(microtime(true) - $start), 1); | |
# actual output: | |
/* | |
before start | |
after start | |
before start | |
ran | |
after start | |
before start | |
ran | |
after start | |
before start | |
ran | |
after start | |
before start | |
ran | |
after start | |
Run time: ~4s | |
ran | |
*/ | |
# expected: | |
/* | |
before start | |
after start | |
before start | |
after start | |
before start | |
after start | |
before start | |
after start | |
before start | |
after start | |
Run time: ~0s | |
<after 1 second, instantly these:> | |
ran | |
ran | |
ran | |
ran | |
ran | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment