douqu8828 2014-03-30 00:08
浏览 40
已采纳

如何使用Phalanger编译pthreads?

So i have pthreads working with PHP on windows, but how can i compile and run my pthreads implementations with phalanger 3.0? At the moment, it builds with 0 errors / 0 warnings, but then when i run it it says

CompileError: The class 'ThreadTest' is incomplete - its base class or interface is unknown in C:\phptests\thread.php on line 10, column 1.

I see in the Phalanger install dir it has the php extensions .dll's; and the php_pthreads zip i downloaded it has .pdb intermediate files for the pthreads .dll's, so is there a way to get Phalanger to compile and run pthreads?

  • 写回答

1条回答 默认 最新

  • dopii22884 2014-03-30 10:22
    关注

    Phalanger does not have a support for pthreads.

    You can use .NET alternatives via clr_create_thread(callback [, parameters]) function or sb. has to implement missing support for pthreads in C#.

    clr_create_thread is bit misleading name though, as it doesn't really create a thread. Instead it takes your callback and schedules it for execution on a ThreadPool. Threads on a thread pool are somewhat special as they do not end when your callback ends. Instead they are reused for later requests (like if you call clr_create_thread again the callback execution may end up on the thread you used previously). As such, there's little sense in Joining ThreadPool threads, as they do not end voluntarily. However, you may use other .net synchronization mechanisms if you want to wait for your callback(s) to finish (AutoResetEvent and WaitHandle::WaitAll are the important parts):

    use System\Threading;
    class ThreadTest
    {
        public static function main()
        {
            (new self)->run();
        }
    
        public function run()
        {
            $that = $this;
    
            $finished = [];
    
            for ($i = 0; $i < 5; $i++) {
                $finished[$i] = new Threading\AutoResetEvent(false);
                clr_create_thread(function() use ($that, $finished, $i) {
                    $that->inathread();
                    $finished[$i]->Set();
                });
            }
    
            Threading\WaitHandle::WaitAll($finished);
            echo "Main ended
    ";
        }
    
        public function inathread()
        {
            $limit = rand(0, 15);
            $threadId = Threading\Thread::$CurrentThread->ManagedThreadId->ToString();
            echo "
     thread $threadId limit: " . $limit . " 
    ";
            for ($i = 0; $i < $limit; $i++) {
                echo "
     thread " . $threadId . " executing 
    ";
                Threading\Thread::Sleep(1000);
            }
            echo "
     thread $threadId ended 
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器