C read and thread safety (linux)

前端 未结 4 1795
南旧
南旧 2020-12-06 01:58

What would happen if you call read (or write, or both) in two different thread, on the same file descriptor (lets says we are interested about a lo

4条回答
  •  死守一世寂寞
    2020-12-06 02:51

    The comment about close is because it doesn't make a lot of sense to close a file descriptor in any situation in which some other thread might be trying to use it. So while it is 'safe' as far as the kernel is concerned, it can lead to odd, hard to diagnose corner cases.

    If a thread closes a file descriptor while a second thread is trying to read from it, the second thread may get an unexpected EBADF error. Worse, if a third thread is simultaneously opening a new file, that might reallocate the same fd, and the second thread might accidentally read from the new file rather than the one it was expecting...

提交回复
热议问题