How does one write thread-safe code segments in PHP? Every counter (using a flat file) I've seen in PHP looks like this:

// read value from file
// increment value
// write value to file

Of course, if you have two clients getting this page, and one of them reads the value before the other writes, you'll have an inaccurate count.

So, generally how do you write thread-safe code in PHP. In JSP you have something like:
synchronize(application) {
[code goes here]
}

Is their a similar construct in PHP, or do you have to roll your own?

Thanks,
John