Multithreaded file recording

I am trying to write to different parts of a large file using multiple threads, as a segmented file loader would do.

My question is: what is the safe way to do this? Open a file for writing, create my streams, transfer a Stream object to each stream? I do not want an error to occur, since multiple threads are accessing the same object at the same time.

This, by the way, is C #.

+3
source share
3 answers

I personally suggest you get data in multiple threads, but actually write to it from one thread. Most likely, it will be much easier. You can use the producer / consumer queue (which is very simple in .NET 4), and each manufacturer will feed the "index, data" pairs. Then the consumer stream could simply sequentially search, write, search, write, etc.

+14
source

If it was programming on Linux, I would recommend that you study the command pwrite()that writes the buffer to the file at a given offset. However, a cursory search for C # documentation does not cause anything like this. Does anyone know if a similar function exists?

+1
source

, , , , - . , , . , , ; , . , , , , , .

+1
source

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


All Articles