Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中跨线程共享全局变量?_Php_Multithreading_Pthreads - Fatal编程技术网

如何在php中跨线程共享全局变量?

如何在php中跨线程共享全局变量?,php,multithreading,pthreads,Php,Multithreading,Pthreads,在多线程中,全局变量或资源在线程之间共享。 我正在c中使用pthread库 #include <stdio.h> #include <pthread.h> #include <unistd.h> void *worker(void *); int ctr = 0; pthread_mutex_t lock; int main(int argc, char *argv[]) { pthread_t t[2]; int i = 0; //~ pth

在多线程中,全局变量或资源在线程之间共享。 我正在c中使用pthread库

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *worker(void *);

int ctr = 0;
pthread_mutex_t lock;

int main(int argc, char *argv[])
{
  pthread_t t[2];
  int i = 0;

//~ pthread_mutex_init(&lock, NULL);

  while(i < 2)
  {
     pthread_create(&t[i], NULL, &worker, NULL);
     i++;
  }

  pthread_join(t[0], NULL);
  pthread_join(t[1], NULL);

//~ pthread_mutex_destroy(&lock);
//~ pthread_join(t[1], NULL);
  return 0;
}

void *worker(void *arg)
{
//~ pthread_mutex_lock(&lock);
//~ int ctr = 0;
    ctr += 1;

    printf("job %d started\n", ctr);
    sleep(1);
//~ ctr += 1;
    printf("job %d finished\n", ctr);

//~ pthread_mutex_unlock(&lock);
}
在该代码中,变量
ctr
在线程之间共享,因此其他线程对该变量所做的更改对另一个线程可见(除非调用了一些
互斥体

我曾在php中尝试过,但没有成功(使用
PHPPThreads
)。 这是我的php代码:

global $ctr;

Class WorkerThread extends Worker
{
    private $thread_id;
    //~ public static $ctr = 0;
    private $mutext;

public function WorkerThread($mutex = NULL)
{ 
    //~ $this->ctr = $ctr;
    $this->mutex = $mutex;
    $this->start();
}

public function run()
{
    //~ Mutex::lock($this->mutex);
    $new_line = php_sapi_name() == "cli" ? PHP_EOL : "<br>";
    //~ global $ctr;
    $ctr = 0;
    $ctr += 1;
    echo "Thread " . $ctr . " started" . " [ " . $this->getThreadId() . " ]" . $new_line;
    sleep(1);
    echo "Thread " . $ctr . " Finished" . " [ " . $this->getThreadId() . " ]" . $new_line;
    //~ var_dump($this);
    //~ Mutex::unlock($this->mutex);
 }
}

//~ $mutex = Mutex::create();
$i = 0;
$worker = array();

while($i < 2)
{
   $worker[$i] = new WorkerThread();
//~ $worker[$i]->start();
   $i++;
}

foreach(range(0, 1) as $t)
   $worker[$t]->join();

//~ Mutex::destroy($mutex);
变量
ctr
(全局)没有像上面的c代码那样由线程更新吗


在php中如何实现这一点(跨线程共享资源)?

通常,线程与启动它们的进程在相同的地址空间中执行

因此,在C中,新线程能够直接访问主程序堆栈上的变量

在PHP中创建新线程时,它有一个单独的堆,必须在单独的地址空间中执行

这意味着默认情况下,您不能在线程之间共享全局状态

这是PHP的正常线程模型-不共享任何内容

pthreads所做的是引入能够在许多上下文中操作的对象,并且能够在这些上下文之间共享数据

等效的PHP代码可能类似于:

<?php
class Atomic extends Threaded {

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

    public function inc() {
        return $this->value++;
    }

    /* ... */
    private $value;
}

class Test extends Thread {

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

    public function run() {
        $this->atomic->inc();
    }

    private $atomic;
}

$atomic = new Atomic();
$threads = [];

for ($thread = 0; $thread < 2; $thread++) {
    $threads[$thread] = new Test($atomic);
    $threads[$thread]->start();
}

foreach ($threads as $thread)
    $thread->join();

var_dump($atomic);
?>
最后,给事物命名的一课

您似乎觉得Posix线程(标准,pthread)和pthreads(PHP扩展)之间有一些相似之处,这是可以原谅的

pthreads
,PHP扩展恰好使用Posix线程,但它没有实现任何类似Posix线程的功能

名称
pthreads
应表示PHP线程。。。给事物命名很难

Thread 1 started [ -1257948352 ]
Thread 1 started [ -1267893440 ]
Thread 1 Finished [ -1257948352 ]
Thread 1 Finished [ -1267893440 ]
<?php
class Atomic extends Threaded {

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

    public function inc() {
        return $this->value++;
    }

    /* ... */
    private $value;
}

class Test extends Thread {

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

    public function run() {
        $this->atomic->inc();
    }

    private $atomic;
}

$atomic = new Atomic();
$threads = [];

for ($thread = 0; $thread < 2; $thread++) {
    $threads[$thread] = new Test($atomic);
    $threads[$thread]->start();
}

foreach ($threads as $thread)
    $thread->join();

var_dump($atomic);
?>
    public function run() {
        $this->atomic->synchronized(function($atomic){
            /* exclusive */
            $atomic->inc();
        }, $this->atomic);
    }