Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 501
ryhzhang Avatar asked Jan 17 '11 07:01

ryhzhang


People also ask

What are PHP threads?

A thread is a small unit of instructions which can be executed by a processor. A application uses threading if it requires parallelism. In other words by a single program ,we can process multiple unit of instructions parallaly.

What is thread and synchronization?

Thread synchronization is the concurrent execution of two or more threads that share critical resources. Threads should be synchronized to avoid critical resource use conflicts. Otherwise, conflicts may arise when parallel-running threads attempt to modify a common variable at the same time.

What is synchronized in PHP?

Threaded::synchronized() function can execute the block while retaining a referenced objects synchronization lock for the calling context. Threaded:: synchronized() function can return a value from the block.

How do you synchronize between threads?

Typically, synchronizing two threads involves the use of one or more synchronization primitives. Synchronization primitives are low-level functions or application objects (not IBM® i objects) that your application uses or creates to provide the synchronization behavior that the application requires.


1 Answers

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.

like image 51
Alfred Avatar answered Sep 27 '22 21:09

Alfred