首页 > 解决方案 > Pthreads 启动不超过一个线程 php

问题描述

我想使用 pthreads,但运行以下脚本每秒只打印一个“1”,它应该每秒回显一个“123”,它只运行第一个线程。

<?php
require "/root/vendor/autoload.php";
set_time_limit( 0 );

function print_time( $id ) {
    while( true ) {
        print( $id );
        sleep( 1 );
    }
}


class Threaded_upload extends Thread {

    public function __construct( $args ) {
        $this->args = $args;

    }

    public function run() {
        print_time( $this->args );
    }
}

$thread_1 = new Threaded_upload( "1" );
$thread_1->start();
$thread_2 = new Threaded_upload( "2" );
$thread_2->start();
$thread_3 = new Threaded_upload( "3" );
$thread_3->start();

提前感谢您的帮助!

标签: phpmultithreadingpthreads

解决方案


推荐阅读