From 163ffb4f546eaf30fb8c88b5491263b381dbacb4 Mon Sep 17 00:00:00 2001 From: coderstephen Date: Fri, 21 Aug 2015 22:39:56 -0500 Subject: [PATCH] Add spawn shortcut for threads --- src/Threading/ThreadContext.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Threading/ThreadContext.php b/src/Threading/ThreadContext.php index caf8d8c..ff938d2 100644 --- a/src/Threading/ThreadContext.php +++ b/src/Threading/ThreadContext.php @@ -28,6 +28,20 @@ class ThreadContext implements ContextInterface */ private $channel; + /** + * Spawns a new thread and runs it. + * + * @param callable $function A callable to invoke in the thread. + * + * @return ThreadContext The thread object that was spawned. + */ + public static function spawn(callable $function /* , ...$args */) + { + $thread = new static($function); + $thread->start(); + return $thread; + } + /** * Creates a new thread context from a thread. *