CentOS 6.3

cd /root

mkdir pthreads

//get php-5.6 and install zts version

wget cn2.php.net/get/php-5.6.11.tar.gz/from/this/mirror

tar zxf /php-5.6.11.tar.gz

cd /php-5.6.11

./configure --prefix=/usr/local/php-zts --with-config-file-path=/usr/local/php-zts/etc --enable-fpm --with-fpm-user=apache --with-fpm-group=apache --enable-mbstring --enable-xml --with-mysql --with-mysqli --with-iconv-dir --enable-maintainer-zts --enable-zip
--enable-pcntl --enable-sockets

make

make install

//get pthreads

wget http://pecl.php.net/get/pthreads

tar zxf pthreads-2.0.10.tgz

cd pthreads-2.0.10

./configure --with-php-config=/usr/local/php-zts/bin/php-confi

make

make install

vi /usr/local/php-zts/etc/php.ini

add:

extension=pthreads.so

cd examples

/usr/local/php-zts/bin/php Mutexes.php

<?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,则是同时多线程执行的。

最新文章

  1. PDFobject插件使用,PDF在线查看插件
  2. CSS控制XML与通过js解析xml然后通过html显示xml中的数据
  3. Python-While刷博爬虫
  4. jquery的end(),addBack()方法example
  5. [转] Asp.Net 导出 Excel 数据的9种方案
  6. [改善Java代码]使用静态内部类提高封装性
  7. Codevs 5059 一起去打CS
  8. HDU 4455(dp)
  9. 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法(android)
  10. Java压缩技术的学习
  11. HDU 4709 Herding 几何题解
  12. Java&amp;Android反编工具打包
  13. Tomcat配置远程调试端口
  14. BOM基础(一)
  15. tcpdump抓包和scp导出以及wireshark查看
  16. sql server 内存初探
  17. kmp算法 模板
  18. [NOIP赛前冲刺第一期]初赛基础知识归纳
  19. ClassNotFoundException和NoClassDefFoundError的区别
  20. 阿里云ubuntu 16.04搭建odoo11服务器

热门文章

  1. 微信小程序-设备
  2. MySQL 5.6 双机热备windows7
  3. 16-腾讯-intership-面试
  4. python走起之第八话
  5. session失效
  6. cocos2dx解决苹果正版ipv6的问题
  7. 进fastreboot
  8. 关于近期项目代码整理(iOS)
  9. XMLHttpRequest.status状态码
  10. js加密解密