Mutex ::在Windows上使用php_pthreads-2.0.9-5.5-ts-vc11-x64在php 5.5.12中解锁退出程序

时间:2015-04-10 18:31:54

标签: php pthreads mutex

在尝试在php中运行pthreads时,我似乎遇到了一个奇怪的问题。我目前正在尝试使用线程来管理其他php脚本的额外调用,但每当我尝试使用Mutex::unlock时,我的程序就会过早退出。我已经尝试重新安装64位pthreads的不同版本以及经过测试的示例(其中,mutex here正常工作)。我认为这可能是popen的一个问题。有没有人在此之前遇到此错误或知道调试它的好方法?

我正在使用的代码如下:

<?php
    class lockedThread extends Thread {
    public function __construct($func, $megalog, $mutex){
        $numargs = func_num_args();
        $this->megalog = $megalog;
        $this->func = $func;
        if ($numargs == 3) {
            $this->mutex = array($mutex);
        } else {
            $arg_list = func_get_args();
            $this->mutex = array_slice($arg_list, 2);
        }
    }

    public function run() {
            echo "Beginning " . $this->func . " \n";
            for ($i = 0; $i < count($this->mutex); $i++) {
                $locked = Mutex::lock($this->mutex[$i]);
                while($locked == false) {
                    sleep(0.5);
                    $locked = Mutex::lock($this->mutex[$i]);
                }
                printf($this->getThreadId() . " acquired lock " . $this->mutex[$i] . "\n");
            }
            $this->res = popen("php Functions/" . $this->func . ".php"
                    . " >> logs/" . $this->func . ".log", 'r');
            printf("closing " . $this->func . ": " . pclose($this->res) . "\n");
            for ($i = count($this->mutex) - 1; $i >= 0; $i -= 1) {
                printf($this->getThreadId() . " attempting to release lock " . $this->mutex[$i] . "\n");
                Mutex::unlock($this->mutex[$i]);
                echo $this->getThreadId() . " released lock " . $this->mutex[$i] . "\n";
            }
            echo "really closing " . $this->func . "\n";
        }        
    }
}   

// Create the mutex. Note, the connections mutex will always come first
$connMutex = Mutex::create(true);
$eventMutex = Mutex::create(true);

$workers["checkRecurrence"] = new lockedThread("checkRecurrence", $megalog, $eventMutex);
$workers["checkRecurrence"]->start();
echo "checkRecurrence is " . $workers["checkRecurrence"]->getThreadId() . "\n";
$workers["checkDeletion"] = new lockedThread("checkDeletion", $megalog, $eventMutex);
$workers["checkDeletion"]->start();
echo "checkDeletion is " . $workers["checkDeletion"]->getThreadId() . "\n";

echo "Unlocking conn: " . Mutex::unlock($connMutex) . "\n";
echo "Unlocking event: " . Mutex::unlock($eventMutex) . "\n";

foreach($workers as $i=>$worker) {
    echo "Joining $i \n";
    while ($worker->join() === false) {
        echo "Attempt of $i \n";
    }
    echo "Joined $i \n";
}

sleep(1);
echo "Finishing destruction of workers!\n";
/* please remember to destroy mutex and condition variables */
Mutex::destroy($connMutex);
Mutex::destroy($eventMutex);

我得到的日志是:

"C:\wamp\bin\php\php5.5.12\php.exe" "C:\wamp\www\hang\PHPAutomation\index.php"
Beginning checkRecurrence 
checkRecurrence is 8576
Beginning checkDeletion 
checkDeletion is 9144
Started all workers 
Unlocking conn: 1
Unlocking event: 1
Joining checkRecurrence 
8576 acquired lock 52687024
closing checkRecurrence: 0
8576 attempting to release lock 52687024
Done.

0 个答案:

没有答案