PHP线程和同步 [英] PHP Threads and Synchronization

查看:99
本文介绍了PHP线程和同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,因此,我决定实现单例.

I'm new to PHP, so to get started I've decided to implement a singleton.

虽然我可以在php中重新创建单例模式,但是我不确定如何实现双重检查锁定.

While I am able to recreate the singleton pattern in php, but I am not sure how to implement double-checked locking.

在PHP中甚至是可能/需要的.我在某处读过PHP不是多线程的?有人可以确认吗?

Is that even possible/needed in PHP. I have read somewhere that PHP is not multithreaded? Can someone confirm that?

如果它是多线程的,有人可以向我解释lock()或sync()在PHP中的工作方式吗?

If it is multithreaded, can someone explain to me how lock() or synchronize() work in PHP?

谢谢, 亨利

推荐答案

无共享架构

PHP的无共享 建筑:

PHP has a Share-nothing Architecture:

  • 就像HTTP一样,每个请求都是不同的
  • 共享数据被下推到数据存储层
  • 避免使用前端控制器
  • Like HTTP, each request is distinct
  • Shared data is pushed down to the data-store layer
  • Avoid front controllers

这给了我们

  • 负载均衡能力
  • 从一个数据中心到另一个数据中心的不可见故障转移
  • 更好的应用程序模块化
  • 更易于开发和调试
  • Ability to load balance
  • Invisible failover from one datacenter to another
  • Better modularization of applications
  • Easier to develop and debug

双重检查锁定

但我不确定如何实施 双重检查锁定.

but I am not sure how to implement double-checked locking.

通常,数据库层对此负责. MySQL(innodb)标准具有例如行级锁定(这应该足够了).

In general the database layer is responsible for this. MySQL(innodb) standard has for example row level locking(which should be sufficient for this).

InnoDB确实在行级别锁定 并以非锁定方式运行查询 默认情况下,在 Oracle的风格.

InnoDB does locking on the row level and runs queries as nonlocking consistent reads by default, in the style of Oracle.

如果这还不止于SQL,例如,

If this is not sufficient than SQL also has for example transactions to make this happen.

在线图书将交易定义为 执行的操作序列为 一个单一的逻辑工作单元"

Books Online defines a transaction as a "sequence of operations performed as a single logical unit of work"

分叉过程

就像幻灯片上所说的那样,PHP具有Share-nothing-Architecture(传统)功能,这也意味着PHP没有默认情况下未启用)PHP具有支持功能到可以相互通信的 fork 进程.当您还编译信号量函数时,您可以执行 sem_acquire

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.

这篇关于PHP线程和同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆