Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronized functions in PHP

Tags:

php

Is there a way to make functions in PHP synchronized to make sure that two or more webusers can't execute the same function at the same time?

like image 726
Chris Avatar asked Dec 28 '10 21:12

Chris


People also ask

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.

What is synchronized used for?

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

What are the types of synchronization?

There are two types of synchronization: full and incremental.

Why synchronized is important?

Data synchronization ensures accurate, secure, compliant data and successful team and customer experiences. It assures congruence between each source of data and its different endpoints. As data comes in, it is cleaned, checked for errors, duplication, and consistency before being put to use.


2 Answers

I think you might achieve the same(if available) by using sem_acquire to acquire a semaphore (entering crital section) and sem_release to release the lock.

like image 184
Alfred Avatar answered Oct 11 '22 17:10

Alfred


You can use external locking - for example, file locking via flock. Create a file somewhere and have script lock it. You can also use semaphores, but those are Unix only.

like image 36
StasM Avatar answered Oct 11 '22 17:10

StasM