duandanxiu6965 2013-01-16 20:07
浏览 119

PHP扩展(在C ++中)函数中的pthread_create永远不会返回

My c++ library create a thread with pthread_create somewhere in the code. Using my library inside a stand-alone application works great, but when using it in PHP extension. The function never returns.

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

The "B" is never printed.

How can I do this?

Thanks!

  • 写回答

2条回答 默认 最新

  • dongmengan8620 2013-01-16 20:22
    关注

    You have a race condition between the newly-created thread printing and the process terminating. You need some kind of synchronization, such as joining the thread before allowing the process to terminate. (Using sleep is okay to demonstrate the problem, but never use sleep as a form of thread synchronization.)

    评论

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备