Can you open the same file several times for writing?

We are writing a multi-threaded application that does a bunch of bit-twisting and writes binary data to disk. Is it possible that each stream has std::fopenthe same file for writing at the same time? The reason may be every thread that could do its job and have its own access to the recorded file.

+4
source share
4 answers

std::fstreamhas functionality defined in terms of the C stdio library. I would be surprised if this was actually indicated, but the most likely behavior when opening the same file twice is several internal buffers associated with the same file descriptor.

- POSIX pwrite writev. C stdio, ++ iostreams. .

: POSIX open, Mac OS X, . , , , , .

:

  • ++ , fstream C (stdio).
  • C , (fopen ).
  • POSIX C.
  • POSIX .

, , , - .

+2

, , .

+1

, , , . , concurrency. , . , ?

0

Access to I / O is not a parallelizable task (it cannot be, you just cannot send two or more data addresses on the device bus at the same time), so you better implement a queue in which many streams put their pieces of data, and one single consumer actually writes them to disk.

0
source

Source: https://habr.com/ru/post/1532237/


All Articles