Can fwrite & fclose be called in parallel from two streams for the same file descriptor?

What happens if fwrite and fclose are called in parallel from two threads for the same file descriptor?

+4
source share
3 answers

fwrite and fclose work with the FILE data structure. Since this is a fairly large data structure that stores more than just a file descriptor, the answer is bad things.

Do not do this if you are not providing the atom with mutexes.

+3
source

POSIX requires FILE access to be thread safe, but since fclose closes the file and invalidates the pointer, there is no way (that is, it is not only a specification issue, but also a basic API problem that can never be “fixed” or done for deletion ) to use fclose when another thread can access FILE . You will need to make your own lock.

+9
source

For this circumstance, you need to prioritize your methods. You can control methods using "synchronized".

-4
source

All Articles