Created
September 14, 2016 11:18
-
-
Save rafaelcalleja/95cc51b3ad4b11255351c5855ce90e01 to your computer and use it in GitHub Desktop.
Running multiple threads example php
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 | |
$pids = array(); | |
for ($i = 0; $i < 10; ++$i) { | |
$pid = pcntl_fork(); | |
if ($pid != 0) { | |
$pids[]=$pid; | |
}else{ | |
$worker = function() use($i) { | |
while (true) { echo "waiting $i\r\n"; sleep(1); } | |
}; | |
$worker(); | |
exit(); | |
} | |
} | |
foreach ($pids as $pid) { | |
pcntl_waitpid($pid, $status); | |
usleep(50000); | |
} | |
/* Expected OUTPUT | |
waiting 0 | |
waiting 1 | |
waiting 2 | |
waiting 3 | |
waiting 4 | |
waiting 5 | |
waiting 6 | |
waiting 7 | |
waiting 8 | |
waiting 9 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment