Reputation: 271
I have a multithreaded application that is opening and reading the same file (not writing). I am opening a different file descriptor for each thread (but they all point to the same file). Each thread then reads the file and may close it and open it again if EOF is reached. Is this ok? If I perform fclose() on a file descriptor does it affect the other file descritptors that point to the same file?
Upvotes: 8
Views: 10317
Reputation: 61
For Linux systems you don't need multiple file descriptors to do this. You can share a single file descriptor and use pread to atomically do a seek / read operation without modifying the file descriptor at all.
Upvotes: 6
Reputation: 57784
That should work fine, provided each thread has its own file handle. Since you mention use of fclose()
, that suggests you are also using fopen()
in each thread and each thread only affects its own FILE *
variable.
Is there a problem?
Upvotes: 0
Reputation: 181430
That's ok. You can open all times you want the same file and each file descriptor will be independent from each other.
Upvotes: 3