3

The PHP flock documentation says it is unsafe on some multithreaded servers like ISAPI, because the lock is a process wide thing, so if one instance of php on thread 1 locks a file another php instance on thread 2 may also lock it because from the OS's point of view the same process already owns the lock.

Now, the PHP docs say this is only a problem on some operating systems, and names ISAPI as an example. My question: can this also happen on linux/apache in multithreaded mode, or is flock safe in that case?

JanKanis
  • 6,346
  • 5
  • 38
  • 42
  • My suggestion would be to try it. Set up a script that opens a file, locks it (`LOCK_EX`), writes data to it and sleeps for 30 seconds. Call it twice. If the file gets the second data write before the 30 seconds has elapsed, no it isn't safe. If the second script can't get its lock until after the first one dies, yes it is safe. Simples. – DaveRandom Jul 10 '12 at 14:46
  • 1
    @DaveRandom: hmm, that's one way to find out, but ubuntu doesn't support thread-safe php so I'll have to compile everything myself. This is for an open source project, so I would like to make it compatible with as many setups as possible. I was hoping someone on SO already knows and tells me. – JanKanis Jul 10 '12 at 15:02

0 Answers0