在PHP中同步和暂停Thread

时间:2014-08-27 17:55:30

标签: php multithreading

我正在同时运行2个线程,但我有一个关键部分,我需要在MySql数据库中放置一些东西。问题是他们可以同时处理相同的事情。

我做了一些计算,显示对于索引20000个不同的新闻页面,索引是从20000到20020.(所以0到20是重复的)

如何在另一个线程访问数据库时暂停一个线程?

-----thread.php
class Process extends Thread {

    public function __construct($website_url){
        $this->website_url = $website_url;
    }

    public function run() {
        work($this->website_url);
    }
}

-------------- work

function work($website_url) {
    while(condition) {
        some work...

        if(something->check){      // if this exist in base
            mysqli->query("INSERT something IN db..."); 
            prepare bind exec...
        }
        // between check and insert, second thread can put that element
        // critical section is really small but sometimes occurs ...
    }
}

------ main.php

$job1 = new Process($website_url,$trigger);
$job2 = new Process($website_url,$trigger);

$job1->start();
$job2->start();

1 个答案:

答案 0 :(得分:6)

互斥

在这里实现目标的最简单方法是使用单个Mutex:

<?php
class Process extends Thread {

    public function __construct($url, $mutex) {
        $this->url = $url;
        $this->mutex = $mutex;
    }

    public function run() {
        work($this->url, $this->mutex);
    }

    protected $url;
    protected $mutex;
}

function work($url, $mutex) {
    while (1) {
        /* some work */

        /* failing to check the return value of calls to acquire
            or release mutex is bad form, I haven't done so for brevity */
        Mutex::lock($mutex);
        {
            /* critical section */
            printf("working on %s\n", $url);

            /* sleeping here shows you that the critical section is 
                not entered by the second thread, this is obviously not needed */
            sleep(1);
        }
        Mutex::unlock($mutex);

        /* breaking here allows the example code to end, not needed */
        break;
    }
}

$website = "stackoverflow.com";

$lock = Mutex::create();

$jobs = [
    new Process($website, $lock),
    new Process($website, $lock)
];

foreach ($jobs as $job)
    $job->start();

foreach ($jobs as $job)
    $job->join();

/* always destroy mutex when finished with them */    
Mutex::destroy($lock);
?>

此代码应该自行解释,我添加了一些注释来指导您完成它。