博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pthreads 2.0.10 test
阅读量:5305 次
发布时间:2019-06-14

本文共 2264 字,大约阅读时间需要 7 分钟。

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

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

转载于:https://www.cnblogs.com/lein317/p/5067520.html

你可能感兴趣的文章
【算法学习】单调队列
查看>>
虚方法和多态
查看>>
团队博客地址
查看>>
master分支合并
查看>>
2017.11.12 web中JDBC 方式访问数据库的技术
查看>>
Pat1128:N Queens Puzzle
查看>>
占位 SC
查看>>
《算法心得:高效算法的奥秘(原书第2版)》
查看>>
知道力——彻底超越执行力的25条职场新思维
查看>>
序号的结构层次顺序
查看>>
AC自动机
查看>>
LCA算法解析-Tarjan&倍增&RMQ
查看>>
BZOJ4241 历史研究 莫队 堆
查看>>
BZOJ1191 [HNOI2006]超级英雄Hero 二分图匹配
查看>>
进制转换2016/3/5
查看>>
片段缓存
查看>>
【转】浅谈JavaScript中forEach与each
查看>>
linux指定目录安装软件后,程序找不到共享库问题
查看>>
php连接、操作mysql数据库
查看>>
hello world!
查看>>