Skip to content

Commit

Permalink
Change example code to remove circular dependency
Browse files Browse the repository at this point in the history
this way is better anyway, since it requires less locks.
  • Loading branch information
dktapps committed Jun 13, 2021
1 parent 303ee83 commit fe5b1db
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class SleepyThread extends \Thread{
/** @var \Threaded */
private $buffer;

public function __construct(\pocketmine\snooze\SleeperNotifier $notifier){
public function __construct(\pocketmine\snooze\SleeperNotifier $notifier, \Threaded $buffer){
$this->notifier = $notifier;
$this->buffer = new \Threaded();
$this->buffer = $buffer;
}

public function run() : void{
Expand All @@ -40,19 +40,16 @@ class SleepyThread extends \Thread{
$this->notifier->wakeupSleeper();
}
}

public function getLineFromBuffer() : ?string{
return $this->buffer->shift();
}
}

$sleeper = new \pocketmine\snooze\SleeperHandler();

$notifier = new \pocketmine\snooze\SleeperNotifier();
$thread = new SleepyThread($notifier);
$sleeper->addNotifier($notifier, function() use($thread) : void{
$buffer = new \Threaded();
$thread = new SleepyThread($notifier, $buffer);
$sleeper->addNotifier($notifier, function() use($buffer) : void{
//do some things when this notifier sends a notification
echo "Main thread got line: " . $thread->getLineFromBuffer();
echo "Main thread got line: " . $buffer->shift();
});

//don't start the thread until we add the notifier, otherwise we could get unexpected behaviour (race conditions)
Expand Down

0 comments on commit fe5b1db

Please sign in to comment.