Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

multithreading - PHP Threads and Synchronization

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Share-nothing Architecture

PHP has a Share-nothing Architecture:

  • Like HTTP, each request is distinct
  • Shared data is pushed down to the data-store layer
  • Avoid front controllers

This gives us:

  • Ability to load balance
  • Invisible failover from one datacenter to another
  • Better modularization of applications
  • Easier to develop and debug

double-checked locking

but I am not sure how to implement 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).

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

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"

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...