php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66349 can't use gobal variables in multi thread
Submitted: 2013-12-25 11:23 UTC Modified: 2013-12-25 13:02 UTC
From: shyxingang at 126 dot com Assigned:
Status: Not a bug Package: pthreads (PECL)
PHP Version: 5.5.7 OS: linux
Private report: No CVE-ID: None
 [2013-12-25 11:23 UTC] shyxingang at 126 dot com
Description:
------------
---
From manual page: http://www.php.net/book.pthreads
---

with pthreads version:  pthreads-0.0.45

Test script:
---------------
<?php

class Buy extends Thread{
    protected $name;

    public function __construct($name){
        $this->name = $name;
    }
    
    public function run(){
        global $xxx,$tickPool;  
        
        echo $xxx."\n\n\n\n\n";
    }
}

$tickPool = new SplQueue();
for($x=0;$x<2;++$x){
    $tickPool->enqueue($x);
}
print_r($tickPool);
$xxx = "abcdefg";

$buyers = array();
for($i=1;$i<=2;$i++){
    $buyers[$i] = new Buy("buyer_".$i);
}
foreach($buyers as $b){
    $b->start();
}

Expected result:
----------------
can see the value of $xxx but not

Actual result:
--------------
$xxx is nothing

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-25 13:02 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2013-12-25 13:02 UTC] krakjoe@php.net
That is intentional behaviour; it is not safe to allow multi-threaded access to global variables since there is no opportunity to implement safety during their manipulation, therefore no steps are taken to make globals available since they are not safe.

The solution is to set data you wish to share between contexts as a member of an object descended from pthreads where every thread that requires access has a reference to the data.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 06:01:26 2019 UTC