flock-ing a C++ ifstream on Linux (GCC 4.6)

后端 未结 4 364
终归单人心
终归单人心 2021-01-13 04:39

context

I\'m slowly writing a specialized web server application in C++ (using the C onion http server library and the JSONCPP library for JSON serialization, if t

4条回答
  •  悲哀的现实
    2021-01-13 04:43

    Is the traditional unix-y solution of relying on the atomicity of rename() unacceptable?

    I mean, unless your JSON serialization format supports in-place update (with a transaction log or whatever), then updating your password database entails rewriting the entire file, doesn't it? So you might as well write it to a temporary file, then rename it over the real name, thus ensuring that readers read a consistent entry? (Of course, in order for this to work each reader must open() the file each time it wants to access a DB entry, leaving the file open doesn't cut it)

提交回复
热议问题