Ivan Vulović
Ivan Vulović

Reputation: 2214

Synchronize and pause Thread in PHP

I am running 2 threads at the same time, but I have critical section where I need to put something in a MySql DB. The problem is that they can put the same thing in at the same time.

I have done some calculations that shows that for indexing 20000 different news pages, the indexes are from 20000 to 20020. (So 0 to 20 are duplicates)

How do I pause one thread while the other is accessing the database?

-----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();

Upvotes: 2

Views: 1485

Answers (1)

Joe Watkins
Joe Watkins

Reputation: 17168

Mutual Exclusion

The simplest way of achieving what you want here is by the use of a single 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);
?>

This code should explain itself, I have added a few comments to guide you through it.

Upvotes: 6

Related Questions