Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What will happen if fwrite & fclose are called in parallel from two threads for the same file descriptor?

like image 992
Jay Avatar asked Jan 22 '23 11:01

Jay


1 Answers

POSIX requires FILE access to be thread-safe, but since fclose 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 use fclose when another thread might be accessing the FILE still. You'll need to do your own locking.

like image 198
R.. GitHub STOP HELPING ICE Avatar answered Jan 25 '23 22:01

R.. GitHub STOP HELPING ICE