duangejian6657
duangejian6657
2015-06-10 02:39

os.File Write()线程安全吗?

已采纳

I was wondering if calling Write() on an os.File is thread safe. I'm having a hard time finding any mention of thread safety in the docs.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duancanjiu3754 duancanjiu3754 6年前

    The convention (at least for the standard library) is the following: No function/method is safe for concurrent use unless explicitly stated (or obvious from the context).

    It is not safe to write concurrently to an os.File via Write() without external synchronization.

    点赞 评论 复制链接分享
  • dongpa5277 dongpa5277 6年前

    After browsing the source code a little bit I found the following method which is eventually called by file.Write(). Since there are race condition checks in place, I'm assuming that the call is in fact not thread-safe within Go (Source).

    However, it seemed unlikely that those system calls wouldn't be thread-safe on an OS level. After some browsing I came upon this interesting answer that fueled my suspicions even more. For windows the source indicates a call to WriteFile which also appears to be thread safe.

    点赞 评论 复制链接分享

相关推荐