Thread safety

I have good expereince in Java/J2EE but I am very new to PHP. There is an small requirement where i want to share value of a variable(which contains a String token)
accross different user requests. Now the issue is that i want to make the read and write of this variable thread safe i. at a time only one user can read/write
in this variable.

If i was to do it in Java i would just take lock on a object by using synchonized keyword and can put the code block(read/write) in synchronzed block.

Here in PHP i have put the token in GLOBALS array but i am not getting any way by which i can made the read and write of this variable thread safe. I am avoiding
to use any external library to avoid depdency. Please guide me how this can be achived.

Web servers are stateless, and therefore any server-side language is stateless too. At the end of processing any request to a web server, all resources created for that instance of a server-side script are destroyed. The php $GLOBALS variable is only global to the current instance of a php script.

If you want to share data between requests for a particular user, you would store the data in session variable(s). If you want to share the data between different users, you would need to store it on the server, either in a flat file (not recommend since you will need to write your own file handling logic with file locking), in a database (the best choice, since the database server will ‘automatically’ provide single record locking/thread safety, or you can use transactions or table locks for multiple records, and the data will be stored persistently should the server be restarted), or in a memory cache.

If this doesn’t address your question, you will need to provide more specific details about what you are actually trying to do.

Sponsor our Newsletter | Privacy Policy | Terms of Service