Multiple threads writing on same file

前端 未结 4 675
说谎
说谎 2020-12-21 10:41

I would like to know if we can use multiple threads to write binary data on the same file.

FILE *fd = openfile(\"test\");
int SIZE = 1000000000;
int * table          


        
相关标签:
4条回答
  • 2020-12-21 11:11

    fwrite should be thread safe, but you'll need a mutex anyway, because you need the seek and the write to be atomic. Depending on your platform, you might have a write function that takes an offset, or you might be able to open the file in each thread. A better option if you have everything in memory anyway as your code suggests, would just be for each thread to fill into a single large array and then write that out when everything is done.

    0 讨论(0)
  • 2020-12-21 11:20

    fseek and fwrite are thread-safe so you can use them without additional synchronization.

    0 讨论(0)
  • 2020-12-21 11:21

    Let each thread open the file, and make sure they write to different positions, finally let each thread close the file and your done.

    Update:

    This works on IX'ish systems, at least.

    0 讨论(0)
  • 2020-12-21 11:23

    While fread() and fwrite() are thread safe, the stream buffer represented by the FILE* is not. So you can have multiple threads accessing the same file, but not via the same FILE* - each thread must have its own, and the file to which they refer must be shareable - which is OS dependent.

    An alternative and possibly simpler approach is to use a memory mapped file, so that each thread treats the file as shared memory, and you let the OS deal with the file I/O. This has a significant advantage over normal file I/O as it is truly random access, so you don't need to worry about fseek() and sequential read/writes etc.

    0 讨论(0)
提交回复
热议问题