4

I am familiar with Java and at the moment I am teaching myself PHP. To prevent race conditions and deadlocks, Java uses the keyword 'synchronized'.

From Oracle docs:

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

I am using prepared statements within a separate class to access my database. I wish to avoid race conditions, deadlocks etc, but I cannot see how PHP handles this.

Does PHP have the equivalent to Java, and is it operating system specific? I am using Windows. What would best practices be?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Alan
  • 279
  • 1
  • 8
  • 19

7 Answers7

5

PHP doesn't do threads. Don't worry about it*.

I'm positive there are reasons that you might be worried about deadlocks and raceconditions, but only if you're handling a large application across many front-ends communicating with the same back-end.

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

tkone
  • 22,092
  • 5
  • 54
  • 78
  • The PHP documentation disagrees with you. http://php.net/manual/en/class.thread.php – Luke A. Leber Jan 22 '17 at 04:05
  • @LukeA.Leber you do realize the answer is over 5 years old, and answered 2 years PRIOR to pthreads being added as PECL extension? Release 0.0.33 (alpha) was 9/25/2012 and the extension isn't marked stable until March 2014? – tkone Jan 22 '17 at 20:01
  • Nope, didn't have a clue. – Luke A. Leber Jan 22 '17 at 23:42
1

In a single threaded application this is no a problem. For the database situation however, I would go with transactions. Transactions will basically do what you would expect fron the synchronized - do several operations in one atomic operation, either all succeed or all failed.l

Dmitri Snytkine
  • 1,096
  • 1
  • 8
  • 14
1

The PHP-file is not run in parallel so within one instance not using the function pcntl-fork a race-condition cannot occur. When you are looking at the side of MySQL, it is fully parallel.

Gustav
  • 2,902
  • 1
  • 25
  • 31
  • could there be an issue from MySQL? or am I missing the point? – Alan Jan 10 '12 at 19:24
  • MySQL is fully async, so no problem. The PHP-script is run from top to bottom so just think of it as a food-recipe or something. ;) – Gustav Jan 10 '12 at 19:29
0

I think the sem_acquire is the best way to do it. "blocks (if necessary) until the semaphore can be acquired. A process attempting to acquire a semaphore which it has already acquired will block forever if acquiring the semaphore would cause its maximum number of semaphore to be exceeded. " http://us3.php.net/manual/en/function.sem-acquire.php

ِAmin
  • 162
  • 1
  • 17
0

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

The pthreads extension is not operating system specific but requires the ZTS (Zend Thread Safety) build of PHP.

Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55
0

yes, with Process Control Extensions http://php.net/manual/ru/threaded.synchronized.php

Artkom
  • 131
  • 1
  • 5
-1

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

See http://us3.php.net/flock for more information on file locking.

Nick Garvey
  • 2,980
  • 24
  • 31