简体   繁体   中英

Can fwrite & fclose be called parallely from two threads for the same file descriptor?

如果从同一文件描述符的两个线程并行调用fwritefclose会发生什么?

POSIX requires FILE access to be thread-safe, but since fclose closes the file and invalidates the pointer, there is no way (ie it's not just a specification issue but a fundamental API issue that could never be "fixed" or made to go away) to use fclose when another thread might be accessing the FILE still. You'll need to do your own locking.

fwrite and fclose operate on the FILE 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.

For such a circumstance you need to define priorities of your methods. You can control the methods with "synchronized".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM