tags:

views:

29

answers:

1

I'm wondering whether methods of PHPs SPLFileObject are atomic (e.g. thread-safe) or not?

If they aren't, I'll implement my own class, which will use flock(), but is this enough? Is the flock function really thread-safe? What if the collision occurs after I fopen() the file, but before I flock() it?

A: 

I think you're misusing the term "thread-safe." Thread saftey is (mostly) about shared resources in threaded code. PHP doesn't have threading, and file handles aren't shared resources. Files are shared resources, though. I think you're looking for the term "race condition" instead.

What if the collision occurs after I fopen() the file, but before I flock() it?

The same thing that would happen if you weren't using SPLFileObject. Just make sure you never open+truncate, always open+append and then truncate once you have the lock. This should be standard procedure if you're already aware of how file opening and locking race conditions work.

Charles