Pthreads and sleep() function

Hi,

Since I’ve upgraded from php 5.6 to 7 my sleep function become not compatible with pthreads. Ok, why not.

I get this error when I call the sleep function:

An uncaught Exception was encountered
Message: sleep is not suitable for use in multi threaded applications, use synchronized Threaded::wait

If this function was called into a Thread class, I can understand and use Threaded::wait. But here the problem that I call this function from a normal php (not into a Thread object)

From the codeigniter controller class:

public function index()
{
    while (true) {
        $tasks = $this->checkTask();
        $thread = array();
        foreach ($tasks as $index => $task) {
            while (array_key_exists($index, $thread))
                $index = rand(0, 65535);
            $thread[$index] = new crwlTasker($task);
            $thread[$index]->start();
        }
        crwlLog("Task launched, wait 60 sec for next check.", 'info', true);
        sleep(60);
    }
}

And I call this script from this command (CLI) php index.php myclass index.

Some Information:
php version 7. (RC5)
codeigniter 3.0.2
pthread version 3.0.9

Thank for reading :slight_smile:

I’ve found a bad solution, but it’s work :smile:.

/**
 * @param float $time seconds
 */
function mySleep($time){
    time_sleep_until(microtime(true)+$time);
}

usleep and sleep trow a exception but not time_sleep_until. :expressionless: Why not … If someone can give me a explanation, please do.

Edit: Another solution on a Github issue.

I think the problem is that PHP considers $thread to be the PHP Thread class
http://php.net/manual/en/class.thread.php

Maybe best to rename your vatible?

No the problem is the sleep function.

How can you be certain ?

Let’s run this command from CLI (command line) php -r "sleep(5);"
That return me some exception. There isn’t other code. Only a simple sleep(5);.

How is it possible ? If I run the same command, it’s work on me.

Don’t forget, I’m on PHP7 (RC5) ZTS with pthreads.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.