<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Method Modifiers</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="stackable.wait.html">Stackable::wait</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.mutex.html">Mutex</a></div> <div class="up"><a href="book.pthreads.html">pthreads</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="pthreads.modifiers" class="chapter"> <h1>Method Modifiers</h1> <p class="para"> pthreads overrides the functionality of the protected and private method modifiers in order to provide functionality more suited to multi-threaded objects. pthreads applies this functionality to all Thread, Worker and Stackable objects from creation. </p> <div class="example" id="example-3709"> <p><strong>Example #1 protected method example: protected methods can only be executed by one Thread at a time.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ExampleThread </span><span style="color: #007700">extends </span><span style="color: #0000BB">Thread </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">run</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">/* thread code */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">synchronized</span><span style="color: #007700">()) {<br /><br /> }<br /> }<br /><br /> protected function </span><span style="color: #0000BB">exclusive</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">/* synchronized method */<br /> </span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">$thread </span><span style="color: #007700">= new </span><span style="color: #0000BB">ExampleThread</span><span style="color: #007700">();<br />if (</span><span style="color: #0000BB">$thread</span><span style="color: #007700">-></span><span style="color: #0000BB">start</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">$thread</span><span style="color: #007700">-></span><span style="color: #0000BB">exclusive</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-3710"> <p><strong>Example #2 private method example: private methods may only be executed by the Thread, Worker, or Stackable during execution</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ExampleThread </span><span style="color: #007700">extends </span><span style="color: #0000BB">Thread </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">run</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">/* thread code */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">insideonly</span><span style="color: #007700">()) {<br /><br /> }<br /> }<br /><br /> private function </span><span style="color: #0000BB">insideonly</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">/* private method */<br /> </span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">$thread </span><span style="color: #007700">= new </span><span style="color: #0000BB">ExampleThread</span><span style="color: #007700">();<br />if (</span><span style="color: #0000BB">$thread</span><span style="color: #007700">-></span><span style="color: #0000BB">start</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">$thread</span><span style="color: #007700">-></span><span style="color: #0000BB">insideonly</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="stackable.wait.html">Stackable::wait</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.mutex.html">Mutex</a></div> <div class="up"><a href="book.pthreads.html">pthreads</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>