Is PHP equivalent to "synchronized" for Java, or is this not required?

I am familiar with Java and currently I am teaching PHP. To prevent race conditions and deadlocks, Java uses the "synchronized" keyword.

From Oracle docs:

public synchronized void increment() { c++; } 

I use prepared instructions in a separate class to access my database. I want to avoid race conditions, deadlocks, etc., but I don’t see how PHP deals with this.

Does PHP have a Java equivalent and is it a specific operating system? I am using windows. What will be the best methods?

+4
source share
7 answers

No, it is best to use " lock ", in this case a file lock.

For more information on file locking, see http://us3.php.net/flock .

-1
source

PHP does not execute threads. Do not worry about it *.

I am sure there are reasons why you can worry about deadlocks and race conditions, but only if you are processing a large application on many interfaces that share the same content.

But, like, yes, don't worry about it.

+5
source

In a single-threaded application, this is not a problem. However, for a database situation, I would go with transactions. Transactions will basically do what you expect from a synchronized one - perform several operations in one atomic operation, either all succeed or all fail.l.

+1
source

The PHP file does not start in parallel, so the pcntl-fork function is not used in one instance, the race state cannot occur. When you look at the MySQL side, it is completely parallel.

+1
source

I think sem_acquire is the best way to do this. "blocks (if necessary) until a semaphore is received. A process trying to acquire a semaphore that it has already acquired will be blocked forever if receiving the semaphore will exceed the maximum number of semaphore." http://us3.php.net/manual/en/function.sem-acquire.php

0
source

If you are writing a multi-threaded application, you can use the pthreads extension, and your multithreaded objects should extend the Threaded class, which has a synchronized method.

The pthreads not operating system dependent, but requires the ZTS (Zend Thread Safety) PHP build.

0
source

yes, with process control extensions http://php.net/manual/en/threaded.synchronized.php

0
source

Source: https://habr.com/ru/post/1390299/


All Articles