CentOS 6.3php
cd /rootmysql
mkdir pthreadssql
//get php-5.6 and install zts version
apache
//get pthreads
多線程
add:socket
extension=pthreads.sothis
/usr/local/php-zts/bin/php Mutexes.php.net
<?php /* this seems like a pretty good way to show you the difference between threads that are syncrhonized with mutex and those that aren't */ /* will show 50 very neat rows <-.........-> then 50 threads doing the same thing with no mutex */ class MyWorkerThread extends Thread { public function __construct($limit, $mutex, $id){ $this->limit = $limit; $this->mutex = $mutex; $this->id = $id; } public function run(){ if($this->mutex) $locked=Mutex::lock($this->mutex); printf("%s#%lu:<-", !empty($locked)?"Y":"N", $this->id); $i=0; sleep(rand(1,3)); while($i++<$this->limit){ echo "."; } printf("->\n"); if($this->mutex) Mutex::unlock($this->mutex); return true; } } $timer = microtime(true); /* create and lock a mutex */ $mutex = Mutex::create(true); /* create workers */ $workers = array(); for($i=0;$i<10;$i++){ $workers[$i]=new MyWorkerThread(rand(30, 100), $mutex,$i); /* they cannot go anywhere, I have the mutex */ $workers[$i]->start(); } printf("Release the (muzzled) hounds ... :\n"); Mutex::unlock($mutex); foreach($workers as $i=> $worker) $workers[$i]->join(); printf("Muzzled: %f seconds\n", microtime(true)-$timer); /* please remember to destroy mutex and condition variables */ Mutex::destroy($mutex); $timer = microtime(true); /* same again, no mutex */ printf("Now no mutex ... :\n"); $workers = array(); for($i=0;$i<10;$i++){ $workers[$i]=new MyWorkerThread(rand(30, 100),null,$i); /* they cannot go anywhere, I have the mutex */ $workers[$i]->start(); } foreach($workers as $worker) $worker->join(); printf("Dribbling: %f seconds\n", microtime(true)-$timer); ?>改了一下,能夠明顯看到用了Mute會是按照順序執行,而不用Mute,則是同時多線程執行的。