开发者

Can multiple threads write into a file simultaneously, if all the threads are writing to different locations?

开发者 https://www.devze.com 2023-04-08 11:07 出处:网络
I am writing the code in c++. Can I run into 开发者_StackOverflowany kind of race conditions or seg-faults?There\'s no problem doing that from the point of view of the underlying system (for all syste

I am writing the code in c++. Can I run into 开发者_StackOverflowany kind of race conditions or seg-faults?


There's no problem doing that from the point of view of the underlying system (for all systems I know). However, typically you would need to have completely separate file descriptors/handles. This is because the file descriptor maintains state, e.g. the current file position.

You also need to check the thread-safety of the particular C++ interface to the filesystem that you are using. This is needed in addition to the thread-safety of the underlying filesystem.

You should also consider the possibility that threaded I/O will be slower. The system may have to serialise access to the bus. You may get better performance from overlapped I/O or a dedicated I/O thread fed through a producer/consumer pipeline.


Another solution, depending on the size of the file and the system you're running on, is to use memory mapped files, ie. mapping the file into virtual memory. This would give you direct access to the file as if it was a piece of memory. This way, any number of threads can simply write to the memory region and subsequent calls to flush the mapping to disk (depending on your configuration of the memory mapping) will simply store the data on disk.

Do note, that due to adressing restrictions on 32 bit platforms, it will not be possible for you to map any file larger than usually 2-3 GB, depending on the architecture and the actual number of bits available to do virtual memory adressing. Most 64 bit systems have 48 bits or more available for this task, allowing you to map at least 256 TB, which I'd take it is more than sufficient.


It depends. Files are not their handles, and streams are not files. This three different concept must be clear.

Now, the operating system can open the file more times returning different handles, each of which have its own "position pointer". If the file is opened in "share mode" for both reading and writing, you can seek the handles where you want and read/write as you like. The fact you don't overwrite depends on you. The system grants the sequentiality of the operations either for the entire file or for part of it (but more information on the operating system are required)

If every handle is attached to a different stream, each stream will write independently of the other. But -in this case- there is the complication of "buffering" (writing can be delayed and read can be anticipated: and can be longer as the one you ask: make sure you manage eventual overlap properly by flushing as appropriate)


Sure you can. Race condition may occur depending on how you are writing actual code(ie using that file). Also, if IO is buffered strange things may appear if buffered regions overlap.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号