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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100