What will happen if fwrite
& fclose
are called in parallel from two threads for the same file descriptor?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
POSIX requires
FILE
access to be thread-safe, but sincefclose
closes the file and invalidates the pointer, there is no way (i.e. it's not just a specification issue but a fundamental API issue that could never be "fixed" or made to go away) to usefclose
when another thread might be accessing theFILE
still. You'll need to do your own locking.For such a circumstance you need to define priorities of your methods. You can control the methods with "synchronized".
fwrite
andfclose
operate on theFILE
data structure. Since this is a largish data structure that does store more than just the file descriptor, the answer is bad things.Don't do this unless you ensure atomic operation using mutexes.