### 实战:实现waitGroup功能 利用Swoole提供的Channel实现一个 waitGroup ,主要功能是用来等待所有协程执行完成的。 ```php <?php class WaitGroup{ private $count; private $chan; public function __construct() { $this->chan = new Swoole\Coroutine\Channel(); } public function add(){ $this->count++; } public function done(){ $this->chan->push(true); } public function wait(){ for($i=0;$i<$this->count;$i++){ $this->chan->pop(); } } } <?php include 'waitgroup.php'; Swoole\Runtime::enableCoroutine(true); echo "start".PHP_EOL; $t = microtime(true); go(function() use ($t){ $wg = new WaitGroup(); $wg->add(); go(function() use ($t,&$wg){ echo file_get_contents("https://www.sunnyos.com/swoole.php"); echo "协程1:".(microtime(true)-$t).PHP_EOL; $wg->done(); }); $wg->add(); go(function() use ($t,&$wg){ echo file_get_contents("https://www.sunnyos.com/swoole.php"); echo "协程2:".(microtime(true)-$t).PHP_EOL; $wg->done(); }); $wg->add(); go(function() use ($t,&$wg){ echo file_get_contents("https://www.sunnyos.com/swoole.php"); echo "协程3:".(microtime(true)-$t).PHP_EOL; $wg->done(); }); $wg->wait(); echo '全部结束:'.(microtime(true)-$t).PHP_EOL; }); echo "end".PHP_EOL; echo microtime(true)-$t.PHP_EOL; ```