会在多线程中检测到套接字错误吗?

huangapple go评论58阅读模式
英文:

will socket error be detected across threads?

问题

The socket is non-blocking and uses epoll. When write encounters error, it will close the socket. My question is, at this point will the read thread return from the epoll wait with error?

英文:

I have two threads working on the same socket, one for read and the other for write. The socket is non-blocking and uses epoll. When write encounters error, it will close the socket. My question is, at this point will the read thread return from the epoll wait with error?

答案1

得分: 2

这种行为是特定于平台的,所以要持怀疑态度。

通常情况下,答案通常是肯定的,但也可能是否定的。

关闭套接字会阻止进一步的读写操作,因此如果写线程决定关闭套接字,读线程就无法从同一个套接字读取。

这通常会导致读取错误。然而,我已经被告知有一些情况下这并不总是成立。所以,最好不要依赖这种行为,如果可以的话。

英文:

This behavior is platform-specific, so take this with a grain of salt.

Generally, the answer is usually yes, but it may be no, too.

Closing a socket prevents any further reading and writing on it, so if the writing thread decides to close the socket, the reading thread can't read from the same socket anymore.

This will usually cause a read error. However, I have been informed of cases here and there where this is not always true. So, best not to rely on this behavior, if you can help it.

huangapple
  • 本文由 发表于 2023年5月23日 01:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定