Advertisement
Guest User

cond2.php

a guest
Jan 11th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. <?php
  2. define("SECOND", 1000000);
  3. define("LOG", Mutex::create());
  4.  
  5. function trace( $sender, $msg )
  6. {
  7. $ts = microtime( true );
  8. Mutex::lock(LOG);
  9. echo date( 'd/m H:i:s', $ts ),
  10. substr( sprintf( "%.6f", $ts - (int) $ts ), 1 ),
  11. ' ',
  12. $sender, ': ', $msg, "\n";
  13. Mutex::unlock(LOG);
  14. }
  15.  
  16. class Test extends Thread
  17. {
  18. public $stopped;
  19.  
  20. public function run()
  21. {
  22. while(true) {
  23. if (!$this->synchronized(function(){
  24. if ($this->stopped || $this->wait(5 * SECOND))
  25. return false;
  26. trace(Thread::getThreadId(), "running" );
  27. return true;
  28. })) {
  29. break;
  30. }
  31. }
  32.  
  33. trace(Thread::getThreadId(), "leaving");
  34. }
  35.  
  36. public function stop()
  37. {
  38. trace(Thread::getThreadId(), "stopping");
  39.  
  40. $this->synchronized(function(){
  41. $this->stopped = true;
  42. $this->notify();
  43. });
  44.  
  45. $this->join();
  46. }
  47. }
  48.  
  49. $test = new Test();
  50. $test->start();
  51. usleep( 5 * SECOND );
  52. $test->stop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement