Is SPLFileObject atomic?

☆樱花仙子☆ 提交于 2019-12-11 02:27:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/3030122/is-splfileobject-atomic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!