环境CentOS 6.3 64bit,php 5.4.5

pthreads需要线程安全环境,

下载php的安装包,解压:

tar zxvf php-5.4.5.tar.gz//名字是不是这个我不确定,自己去下载

安装:

./configure --enable-zts --prefix=/usr/local/php-zts //或者 --enable-maintainer-zts

make

make install

结果就把线程安装版的php安装到了/usr/local/php-zts,目前我不了解开启线程安全到底会带来什么影响,所以,就弄两个测试版本。

如果遇到undefined reference to `executor_globals_id'

make clean就可以了

下载和安装pthreads:

tar zxvf pthreads

cd pthreads-0.0.45 // 本文的是第一个stable版本

/usr/local/php-zts/bin/phpize

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

make

make install

/usr/local/php-zts/bin/php -i|grep "php.ini"

找出php.ini的位置,修改它把ext=pthreads.so加入进入进去,因为我原来就有php,现在新装的php.ini文件不存在我就考了一个过去……

pthreads-0.0.45/examples目录下有些示例文件,比文档有用,在线文档根本不能说明问题,我修改了一个,执行了一下:

<?php
/*
* Sharing symbols 101
* @NOTE Thread::fetch was never included in a release and was superceeded by object handlers
* pthreads allows read access to thread data from any context
pthreads allows write access to thread data from any context
carry on reading ...
work in progress ...
*/
class TestObject {
public $val;
} class Fetching extends Thread {
public function run(){
/*
* of course ...
*/
$this->sym = 10245;
$this->arr = array(
"1", "2", "3"
);
echo '6'.chr(10);
/*
* objects do work, no preparation needed ...
* read/write objects isn't finalized ..
* so do the dance to make it work ...
*/
$obj = new TestObject();
$obj->val = "testval";
$this->obj = $obj;
echo '7'.chr(10);
/*
* will always work
*/
$this->objs = serialize($this->obj);
echo '8'.chr(10);
/*
* nooooooo
*/
$this->res = fopen("php://stdout", "w");
echo '9'.chr(10);
/*
* tell the waiting process we have created symbols and fetch will succeed
*/
$this->synchronized(function(){
$this->notify();
});
echo '10'.chr(10);
/* wait for the process to be finished with the stream */
$this->synchronized(function(){
$this->wait();
});
echo '11'.chr(10);
}
}
echo '0'.chr(10);
$thread = new Fetching();
echo '1'.chr(10);
$thread->start();
//sleep(1); 加了这个就不能正常工作,真奇怪
echo '2'.chr(10);
$thread->synchronized(function($me){
echo '3'.chr(10);
$me->wait();
echo '4'.chr(10);
}, $thread);
echo '5'.chr(10); /*
* we just got notified that there are symbols waiting
*/
foreach(array("sym", "arr", "obj", "objs", "res") as $symbol){
printf("\$thread->%s: ", $symbol);
$fetched = $thread->$symbol;
if ($fetched) {
switch($symbol){
/*
* manual unserialize
*/
case "objs":
var_dump(unserialize($fetched));
break; default: var_dump($fetched);
}
}
printf("\n");
} /* notify the thread so it can destroy resource */
$thread->synchronized(function($me){
$me->notify();
}, $thread);
?>

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

结果:

0

1

2

3

6

7

8

9

10

4

5

$thread->sym: int(10245)

 

$thread->arr: array(3) {

  [0]=>

  string(1) "1"

  [1]=>

  string(1) "2"

  [2]=>

  string(1) "3"

}

 

$thread->obj: object(TestObject)#2 (1) {

  ["val"]=>

  string(7) "testval"

}

 

$thread->objs: object(TestObject)#2 (1) {

  ["val"]=>

  string(7) "testval"

}

 

$thread->res: resource(3) of type (stream)

 

11

发个全点儿的php编译参数:

./configure

--prefix=/usr/local/php-zts

--enable-sockets

--enable-pcntl

--enable-maintainer-zts

--enable-sysvmsg

--enable-mbstring

--with-mysql=/usr

--with-mysql-sock=/var/lib/mysql/mysql.sock  

--with-mysqli=/usr/lib64/mysql/mysql_config

--with-pdo-mysql=/usr/lib64/mysql/mysql_config/configure

我的QQ群:

PHPer&Webgame&移动开发,群号:95303036

windows版:

http://blog.csdn.net/aoyoo111/article/details/19020161

PHP 多线程扩展(正儿八经的线程)pthreads安装的更多相关文章

  1. Windows下PHP多线程扩展pthreads的安装

    pthreads扩展安装步骤 1.查看phpinfo() 获取PHP版本号及位数(x86表示32位,x64表示64位).编译器版本.PHP配置文件加载所在位置等.如下图所示: 2.pthreads扩展 ...

  2. Java多线程(二) —— 线程安全、线程同步、线程间通信(含面试题集)

    一.线程安全 多个线程在执行同一段代码的时候,每次的执行结果和单线程执行的结果都是一样的,不存在执行结果的二义性,就可以称作是线程安全的. 讲到线程安全问题,其实是指多线程环境下对共享资源的访问可能会 ...

  3. CoreData和SQLite多线程访问时的线程安全

    关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的.在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱.IOS中常 ...

  4. C#多线程之旅(3)——线程池

    v博客前言 先交代下背景,写<C#多线程之旅>这个系列文章主要是因为以下几个原因:1.多线程在C/S和B/S架构中用得是非常多的;2.而且多线程的使用是非常复杂的,如果没有用好,容易造成很 ...

  5. (转).NET 4.5中使用Task.Run和Parallel.For()实现的C# Winform多线程任务及跨线程更新UI控件综合实例

    http://2sharings.com/2014/net-4-5-task-run-parallel-for-winform-cross-multiple-threads-update-ui-dem ...

  6. 面试题_1_to_16_多线程、并发及线程的基础问题

    多线程.并发及线程的基础问题 1)Java 中能创建 volatile 数组吗?能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引 ...

  7. C#多线程实践——锁和线程安全

    锁实现互斥的访问,用于确保在同一时刻只有一个线程可以进入特殊的代码片段,考虑下面的类: class ThreadUnsafe { static int val1, val2; static void ...

  8. C# 多线程的自动管理(线程池) 基于Task的方式

    C# 多线程的自动管理(线程池) 在多线程的程序中,经常会出现两种情况:    1. 应用程序中线程把大部分的时间花费在等待状态,等待某个事件发生,然后给予响应.这一般使用 ThreadPool(线程 ...

  9. 多线程编程-- part 2 线程的生命周期和优先级

    线程的创建到消亡的历程: java多线程的5种状态: (1)New(新建) new Thread(run()) 该线程还没开始运行,状态是new,在程序运行前还有一些基础工作要做 (2)runnabl ...

  10. Java多线程(一) —— 线程的状态详解

    一.多线程概述  1. 进程 是一个正在执行的程序.是程序在计算机上的一次运行活动. 每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控制单元. 系统以进程为基本单位进行系统资源的调度 ...

随机推荐

  1. Web缓存的作用与类型

    前言 Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在于Web服务器和客户端(浏览器)之间的副本.缓存会根据进来的请求保存输出内容的副本:当下一个请求来到的时候,如果是相同的UR ...

  2. Android图形显示之硬件抽象层Gralloc(hal 转)

    原文  http://blog.csdn.net/yangwen123/article/details/12192401 FrameBuffer驱动程序分析 文中介绍了Linux系统下的显示驱动框架, ...

  3. hdu 1058 Humble Numbers

    这题应该是用dp来做的吧,但一时不想思考了,写了个很暴力的,类似模拟打表,然后排序即可,要注意的是输出的格式,在这里wa了一发,看了别人的代码才知道哪些情况没考虑到. #include<cstd ...

  4. CSS3_边框属性之圆角的基本图形案例

    一.正方形 div{ background:#F00; width:100px; height:100px;}   二.长方形 div{background:#F00;width:200px;heig ...

  5. RAC 集群更换IP

    RAC 集群更换 IP 主要分三步:停集群服务.配置服务器网络.修改集群配置.下面是同网段内更换 IP 示例.(r7.r8为服务器名称,orcl为ORACLE_SID,scanip为 scan 名称) ...

  6. 每日一笔记之3:QTconnect()

    刚学习QT的时候,跟着教程做一些简答的实验,教程简单的界面使用UI文件,直接在界面上拖一个按键,在右键go to slot,在编写槽函数. 我以前没学过C++,一直以为这个自动跳转过去的slot函数是 ...

  7. Unix domain sockets

    #server: SERVER_PATH = "/tmp/python_unix_socket_server" def run_unix_domain_socket_server( ...

  8. 各种element/format 在manage display 下的选项

    long text = > plain text, summary and trimmed, trimmed,default, hiddenentity refernece => enti ...

  9. Nodejs 配置+基础

    Nodejs + NPP 配置. http://blog.csdn.net/foruok/article/details/48366765 NPM的全称是Node Package Manager,它就 ...

  10. Unity3D 5.1烘培 操作

    http://blog.csdn.net/asd237241291/article/details/48056575 原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 Unity3D引擎技 ...