*/ private ThreadSafeArray $threads; private function __construct(){ $this->threads = new ThreadSafeArray(); } public function add(Worker|Thread $thread) : void{ $this->threads[spl_object_id($thread)] = $thread; } public function remove(Worker|Thread $thread) : void{ unset($this->threads[spl_object_id($thread)]); } /** * @return Worker[]|Thread[] */ public function getAll() : array{ $array = []; /** * @var Worker|Thread $thread */ foreach($this->threads as $key => $thread){ $array[$key] = $thread; } return $array; } public function stopAll() : int{ $logger = \GlobalLogger::get(); $erroredThreads = 0; foreach($this->getAll() as $thread){ $logger->debug("Stopping " . $thread->getThreadName() . " thread"); try{ $thread->quit(); $logger->debug($thread->getThreadName() . " thread stopped successfully."); }catch(ThreadException $e){ ++$erroredThreads; $logger->debug("Could not stop " . $thread->getThreadName() . " thread: " . $e->getMessage()); } } return $erroredThreads; } }