function progress($ch) { // infinite loop for (;;) { echo "still downloadin'...\n"; // the second argument to thread_message_queue_poll is a timeout // if it times out, it returns the string (not the constant) PHP_THREAD_POLL_TIMEOUT // this function is blocking (it waits until a message is recieved or timeout triggered) $var = thread_message_queue_poll($ch, 500); if ($var == 'done') { return; } } }
function sub($i, $ch) { for (;;) { // receive the message from $ch $a = thread_message_queue_poll($ch); // TRIPLE COMPARISION IS IMPORTANT!!!! if ($a === 'PHP_THREAD_POLL_STOP') { break; } printf("thread %02d: %02s\n", $i, $a * 2); } sleep(2); printf("thread %02s is done.\n", $i); }