PHP 线程和同步
我是 PHP 新手,所以首先我决定实现一个单例。
虽然我能够在 php 中重新创建单例模式,但我不确定如何实现双重检查锁定。
这在 PHP 中是否可能/需要?我在某处读到 PHP 不是多线程的?有人可以证实吗?
如果是多线程,有人可以向我解释lock()或synchronize()在PHP中如何工作吗?
谢谢, 亨利
I'm new to PHP, so to get started I've decided to implement a singleton.
While I am able to recreate the singleton pattern in php, but I am not sure how to implement double-checked locking.
Is that even possible/needed in PHP. I have read somewhere that PHP is not multithreaded? Can someone confirm that?
If it is multithreaded, can someone explain to me how lock() or synchronize() work in PHP?
Thanks,
Henry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无共享架构
双重检查锁定
一般来说,数据库层负责这一点。 MySQL(innodb) 标准具有例如行级别 锁定< /a>(这应该足够了)。
如果这还不够,那么 SQL 还具有例如 交易来实现这一点。
Fork 进程
就像幻灯片中所说的 PHP 有一个 Share-nothing-Architecture(传统),这也意味着 PHP 没有 线程(模型)。虽然你可以编译(默认情况下不启用) PHP 支持 fork 可以相互通信的进程。当您还编译 信号量函数 然后你可以做类似 sem_acquire< /a> 和 sem_release 但通常情况下不会。应用PHP。
Share-nothing Architecture
double-checked locking
In general the database layer is responsible for this. MySQL(innodb) standard has for example row level locking(which should be sufficient for this).
If this is not sufficient than SQL also has for example transactions to make this happen.
Fork processes
Like the slides say PHP has a Share-nothing-Architecture(traditional) which also does imply that PHP does NOT have a thread(model). Although you can compile(not enabled by default) PHP to have support to fork processes which can communicate with each other. When you also compile the Semaphore Functions then you can do things like sem_acquire and sem_release. But in general this does not apply PHP.