Skip to content

Instantly share code, notes, and snippets.

@amcsi
Created April 8, 2014 21:46
Show Gist options
  • Save amcsi/10197791 to your computer and use it in GitHub Desktop.
Save amcsi/10197791 to your computer and use it in GitHub Desktop.
Pthreads sleep() experiment
<?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