Is ofstream thread safe?

后端 未结 3 1051
你的背包
你的背包 2020-12-10 19:00

I am working on a program, which uses multiple std::ifstreams for reading a binary file, one std::ifstream for each thread. Now I need to know, if

3条回答
  •  爱一瞬间的悲伤
    2020-12-10 19:43

    Yes. It is.

    For Windows: it is safe to write to fstream from multiple threads on windows. Please see the msdn document: Thread Safety in the C++ Standard Library

    For Linux: In short, it is. From the document of libstdc++: "if your platform's C library is threadsafe, then your fstream I/O operations will be threadsafe at the lowest level". Is your platform's C library threadsafe? Yes. The POSIX standard requires that C stdio FILE* operations(such as fread/fwrite) are atomic, and glibc did so.

提交回复
热议问题