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
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.
fseek and fwrite are thread-safe so you can use them without additional synchronization.
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.
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.