Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define("SECOND", 1000000);
- define("LOG", Mutex::create());
- function trace( $sender, $msg )
- {
- $ts = microtime( true );
- Mutex::lock(LOG);
- echo date( 'd/m H:i:s', $ts ),
- substr( sprintf( "%.6f", $ts - (int) $ts ), 1 ),
- ' ',
- $sender, ': ', $msg, "\n";
- Mutex::unlock(LOG);
- }
- class Test extends Thread
- {
- public $stopped;
- public function run()
- {
- while(true) {
- if (!$this->synchronized(function(){
- if ($this->stopped || $this->wait(5 * SECOND))
- return false;
- trace(Thread::getThreadId(), "running" );
- return true;
- })) {
- break;
- }
- }
- trace(Thread::getThreadId(), "leaving");
- }
- public function stop()
- {
- trace(Thread::getThreadId(), "stopping");
- $this->synchronized(function(){
- $this->stopped = true;
- $this->notify();
- });
- $this->join();
- }
- }
- $test = new Test();
- $test->start();
- usleep( 5 * SECOND );
- $test->stop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement