zoukankan      html  css  js  c++  java
  • pthreads 2.0.10 test

    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("->
    ");
    		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 ... :
    ");
    Mutex::unlock($mutex);
    foreach($workers as $i=> $worker)
    	$workers[$i]->join();
    printf("Muzzled: %f seconds
    ", 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 ... :
    ");
    $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
    ", microtime(true)-$timer);
    ?>

    改了一下,可以明显看到用了Mute会是按照顺序执行,而不用Mute,则是同时多线程执行的。


  • 相关阅读:
    libmv
    visualSFM
    opencv学习笔记——时间计算函数getTickCount()和getTickFrequency()
    opencv学习笔记——cv::mean()函数详解
    linux使用ip能ping通,但使用域名却不能访问的解决方法
    yum 安装出错--"Couldn't resolve host 'mirrors.aliyun.com'"
    vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0'
    VMWare虚拟机 网络连接模式
    js监听input输入框值的实时变化实例
    本地连接linux虚拟机的方法
  • 原文地址:https://www.cnblogs.com/lein317/p/5067520.html
Copyright © 2011-2022 走看看