C read and thread safety (linux)

前端 未结 4 1790
南旧
南旧 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:33

    Any system level (syscall) file descriptor access is thread safe in all mainstream UNIX-like OSes. Though depending on the age they are not necessarily signal safe.

    If you call read, write, accept or similar on a file descriptor from two different tasks then the kernel's internal locking mechanism will resolve contention.

    For reads each byte may be only read once though and writes will go in any undefined order.

    The stdio library functions fread, fwrite and co. also have by default internal locking on the control structures, though by using flags it is possible to disable that.

提交回复
热议问题