diff --git a/examples/fork.php b/examples/fork.php deleted file mode 100755 index ab1a597..0000000 --- a/examples/fork.php +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env php -send('Data sent from child.'); - - print "Child sleeping for 2 seconds...\n"; - sleep(2); - - return 42; - }); - - $timer = Loop::repeat(1000, function () use ($context) { - static $i; - $i = $i ? ++$i : 1; - print "Demonstrating how alive the parent is for the {$i}th time.\n"; - }); - - try { - printf("Received the following from child: %s\n", yield $context->receive()); - printf("Child ended with value %d!\n", yield $context->join()); - } catch (Throwable $e) { - print "Error from child!\n"; - print $e."\n"; - } finally { - Loop::cancel($timer); - } -}); diff --git a/examples/parcel.php b/examples/parcel.php new file mode 100755 index 0000000..2293dd1 --- /dev/null +++ b/examples/parcel.php @@ -0,0 +1,40 @@ +#!/usr/bin/env php +synchronized(function (int $value) { + return $value + 1; + }); + + printf("Value after modifying in child thread: %s\n", $value); + + yield new Delayed(2000); // Main thread should access parcel during this time. + + // Unwrapping the parcel now should give value from main thread. + printf("Value in child thread after being modified in main thread: %s\n", yield $parcel->unwrap()); + + yield $parcel->synchronized(function (int $value) { + return $value + 1; + }); + }, $parcel); + + yield new Delayed(1000); // Give the thread time to start and access the parcel. + + yield $parcel->synchronized(function (int $value) { + return $value + 1; + }); + + yield $context->join(); // Wait for child thread to finish. + + printf("Final value of parcel: %d\n", yield $parcel->unwrap()); +}); diff --git a/examples/thread.php b/examples/thread.php index ff01e7f..447fd1e 100755 --- a/examples/thread.php +++ b/examples/thread.php @@ -4,7 +4,7 @@ require dirname(__DIR__).'/vendor/autoload.php'; use Amp\Delayed; use Amp\Loop; -use Amp\Parallel\Thread\Thread; +use Amp\Parallel\Context\Thread; Loop::run(function () { $timer = Loop::repeat(1000, function () {