Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Created January 24, 2020 04:33
Show Gist options
  • Select an option

  • Save Muqsit/88ef84765e55e32f446ea9762c428c29 to your computer and use it in GitHub Desktop.

Select an option

Save Muqsit/88ef84765e55e32f446ea9762c428c29 to your computer and use it in GitHub Desktop.
test
<?php
require "vendor/autoload.php";
use pocketmine\snooze\SleeperNotifier;
use pocketmine\snooze\SleeperHandler;
class MyThread extends \Thread{
private $notifier;
public $result;
public function __construct(SleeperNotifier $notifier){
$this->notifier = $notifier;
}
public function executeQuery() : void{
$this->synchronized(function() : void{
$this->notify();
});
}
public function run() : void{
while(true){
$this->result = mt_rand() / mt_getrandmax();
$this->notifier->wakeupSleeper();
$this->synchronized(function() : void{
$this->wait();
});
}
}
public function setGarbage(){
}
}
$handler = new SleeperHandler();
$notifier = new SleeperNotifier();
$thread = new MyThread($notifier);
$handler->addNotifier($notifier, function() use($thread) : void{
echo "Received result: " . $thread->result . PHP_EOL;
});
$thread->start();
$i = 0;
while(++$i <= 10){
$thread->executeQuery();
$handler->sleepUntilNotification();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment