Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Unable to store Stream resources in Thread object #39

Closed
chibisuke opened this issue Jan 11, 2013 · 6 comments
Closed

Unable to store Stream resources in Thread object #39

chibisuke opened this issue Jan 11, 2013 · 6 comments

Comments

@chibisuke
Copy link
Contributor

As soon as I assign a stream resource to a thread object member the stream resource gets somehow damaged.

class fw_reader extends Thread {
    public function run() {
        $stream = fopen('c:\windows\system32\drivers\etc\hosts','r');
        $this->socket = $stream;
        stream_set_blocking($this->socket, true);
        stream_set_timeout($this->socket, 2);
    }
}

$fw_r = new fw_reader();
$fw_r->start();
$fw_r->join();

error message is:
Warning: stream_set_timeout(): 2 is not a valid stream resource in P:\php\test.php on line 6

Using PHP 5.4, pthreads [245dbb1] on windows

If I change $this->socket to $stream it works fine.Also if I reverse the order of the "steam_set_blocking" and the "stream_set_timeout the error happens on the stream_set_blocking command and if I add a print_r($this->socket) before the steam_set_ it happens on both.

Any idea?

EDIT: I can confirm the same problem also happening with Stackable objects (instread Thread), while it works if using global scope

@krakjoe
Copy link
Owner

krakjoe commented Jan 11, 2013

snapshots waiting for testing @ http://pthreads.org/snapshots

@chibisuke
Copy link
Contributor Author

Thx for trying to fix this, unfortunately the fix seams to be incomplete in 2e4f64b

The following example still fails in the same way.

<?php
class Work extends Stackable {
public function run(){

}
}

class Test extends Thread {
public function run(){
$test = new Work();
$this->test = $test;
print_r($this->test);
var_dump($this->test);
$stream = fopen("d:\\test.txt", "w+");
var_dump($stream);
$this->stream = $stream;
fwrite($this->stream, "test");
fwrite($this->stream, "test");
fwrite($this->stream, "test");
var_dump($this->stream);
}
}

$test =new Test();
$test->start();
?>

krakjoe added a commit that referenced this issue Jan 12, 2013
@krakjoe
Copy link
Owner

krakjoe commented Jan 12, 2013

:)

@torstahl
Copy link

torstahl commented Feb 6, 2015

Helllo

i have the same Problem on php5-5.4.20 with pthreads 2.0.10

class Joy {
public $device = '/tmp/test.txt';
public $stream;

public function __construct()
{
}

public function open()
{
    $this->stream = fopen($this->device,'rb');
    stream_set_blocking ($this->stream,0);
    var_dump($this->stream);
} // end function open

public function read()
{
    var_dump($this->stream);
    echo $line = stream_get_contents($this->stream,8)."\n";
}

/**
 *
 */
public function close()
{
    fclose($this->stream);
} // end function close

}

class Test extends Thread {

private $xr;

public function run()
{
    // works
    $joystick = new Joy();
    $joystick->open();
    $joystick->read();
    $joystick->close();

    // don't work
    $this->xr = new Joy();
    $this->xr->open();
    $this->xr->read();
    $this->xr->close();

}
}

$test =new Test();
$test->start();

stream is NULL when using this.

Best Regards
Torsten

@rdlowrey
Copy link

rdlowrey commented Feb 6, 2015

In passing I noticed the examples here are messing with blocking flags on filesystem resources. As a public service announcement:

Setting non-blocking mode on a filesystem resource will have no effect. It's a waste of time whether or not you're using pthreads.

Here's a good read on the subject:

http://www.remlab.net/op/nonblock.shtml

@torstahl
Copy link

torstahl commented Feb 6, 2015

Hello
i use this to read from a joystick device /dev/input/js0
so for the report the stream_set_blocking is not necessary.

BR/Torsten

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants