C read and thread safety (linux)

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

    The result would depend on how the threads are scheduled to run at that particular instant in time.

    One way to potentially avoid undefined behavior with multi-threading is to assume that you are doing memory operations. E.g. updating a linked list or changing a variable, etc.

    If you use mutex/semaphores/lock or some other synchronization mechanism, it should work as intended.

提交回复
热议问题