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

pthreads 2.0.10 test的更多相关文章

  1. CentOS7 编译安装 Nodejs (实测 笔记 Centos 7.0 + node 0.10.33)

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...

  2. Time.deltaTime 的平均值在0.1-0.2左右

    Time.deltaTime 平均值在0.1-0.2左右 低的在0.03 高的在0.3

  3. kafka0.9.0及0.10.0配置属性

    问题导读1.borker包含哪些属性?2.Producer包含哪些属性?3.Consumer如何配置?borker(0.9.0及0.10.0)配置Kafka日志本身是由多个日志段组成(log segm ...

  4. how to build apache log4cxx 0.10 by Visual Studio 201*

    Chapter 1 Official Steps  We are going to follow the steps here, http://logging.apache.org/log4cxx/b ...

  5. Microsoft.Bcl.Build 1.0.10 稳定版发布

    Microsoft.Bcl.Build 1.0.10 稳定版发布 解决了之前 1.0.8 在未下载相应的Nuget Package 的情况下项目无法加载的情况 但由于 Microsoft.Net.Ht ...

  6. 小红伞和virtualbox5.0.10冲突

    win7 sp1 64bit 旗舰版:virtual box 5.0.10 提示 error in supr3hardNtChildWaitFor……Timed out after 60001 ms ...

  7. Kafka 0.10.0

    2.1 Producer API We encourage all new development to use the new Java producer. This client is produ ...

  8. DotNetBar v12.7.0.10 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.7.0.10 如果遇到破解问题可以与 ...

  9. (原创)win7自带IIS7.5+php7.0.10安装教程(图)

    php在上周8月18日发布了PHP 7.0 (7.0.10)版本.详细下载页面http://windows.php.net/download/,根据自身电脑配置情况酌情下载版本.win7旗舰版,iis ...

随机推荐

  1. slide效果

    html和js部分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  2. LRU页面置换算法

    本文以序列长度20的{ 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};以及页面4:为例: #include <stdio.h> #define Init ...

  3. Java Web之会话管理二:Session

    一.Session 在web开发中,服务器可以为每个yoghurt浏览器创建一个会话对象(Session)对象.注意:一个浏览器独占一个Session对象.因此,在需要保存用户数据时,服务器程序可以把 ...

  4. 封装的ajax请求

    在做登录注册这类提交表单数据时,我们经常需要局部刷新网页来验证用户输入的信息,这就需要用到ajax请求,我们通常需要获取表单中的数据,发起ajax请求,通过服务程序,与数据库的数据进行比对,判断信息的 ...

  5. BZOJ 4517: [Sdoi2016]排列计数

    4517: [Sdoi2016]排列计数 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 911  Solved: 566[Submit][Status ...

  6. 在C#中读取枚举值的描述属性

    枚举: public enum EnumLanugage { [System.ComponentModel.Description("中文")] Chinese, English ...

  7. php : DOM 操作 XML

    DOM 操作 XML 基本用法 XML文件: person.XML <?xml version="1.0" encoding="utf-8" ?> ...

  8. Strus2学习:基础(一)

    Strus2基础: Sturs2起源以及背景: 在起源很早(2002年左右)的 strus1 和 webWork 基础上进行扩展,并且兼容这两大框架!总之很好用啦,随着学习的深入,应该会有更好的诠释的 ...

  9. 慕课网__css_padding && z_index

    一个正方形 对于“内联元素”来说 z-index 静态布局没有z-index作用

  10. Java多线程同步 synchronized 关键字的使用

    代表这个方法加锁,相当于不管哪一个线程A每次运行到这个方法时,都要检查有没有其它正在用这个方法的线程B(或者C D等),有的话要等正在使用这个方法的线程B(或者C D)运行完这个方法后再运行此线程A, ...