gpt4 book ai didi

php - PHP 扩展(在 C++ 中)函数中的 pthread_create 从不返回

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:00:17 27 4
gpt4 key购买 nike

我的 C++ 库在代码的某处使用 pthread_create 创建了一个线程。在独立应用程序中使用我的库效果很好,但在 PHP 扩展中使用它时。该函数永远不会返回。

void* threadloop(void * param)
{
zend_printf("B\n");
}
PHP_FUNCTION(create_thread)
{
pthread_t othread;
pthread_create (&othread, NULL, threadloop, NULL);
zend_printf("A\n");
}

“B”从不打印。

我该怎么做?

谢谢!

最佳答案

在新创建的线程打印和进程终止之间存在竞争条件。您需要某种同步,例如在允许进程终止之前加入线程。 (使用 sleep 可以演示问题,但永远不要将 sleep 用作线程同步的一种形式。)

关于php - PHP 扩展(在 C++ 中)函数中的 pthread_create 从不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14366886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com