【发布时间】:2017-03-18 18:40:31
【问题描述】:
我有如下代码,使用 php7-zts 和 pthreads。我想知道如何正确清理完成的客户端,以免出现内存泄漏。
<?php
class Client extends Thread {
public function __construct($socket){
$this->socket = $socket;
$this->start();
}
public function run(){
$msgsock = $this->socket;
if ($msgsock) {
if (false === ($buf = socket_read($msgsock, 2048, PHP_BINARY_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
}
......
$write_result = socket_send($msgsock, $result, strlen($result), MSG_EOF);
socket_close($msgsock);
//finished
}
}
}
...
//create, bind, and listen $sock
...
while(($client = socket_accept($sock))){
$clients[]=new Client($client);
}
?>
【问题讨论】:
标签: php multithreading sockets pthreads