On Monday, 26 March 2018 at 16:14:31 UTC, Jonathan wrote:
Can I send data over an std.socket on multiple threads without manual mutexing?

If not, can I send data on a separate thread than receive?

The docs for std.socket say nothing of it (which I guess means I should assume it is not thread safe but...).

It’s thin-wrapper over system interface.

It’s thread-safe in that system will not topple over, you will likely get some interleaving of data.

HOWEVER: there is such a thing as short-writes, which means that since OS buffer may fill up it could write less then you intended to and return that amount. It certainly happens with O_NONBLOCK. In normal blocking mode it might work, but in general it’s a bad idea to do writes to a single socket from multiple threads w/o some coordination s.t. only can do it at any given time.

Lastly as far as guarantees go, POSIX doesn’t state that 2 writes will be interleaved “exactly”, it may for instance splice them zebra-style by some chunk size and I could imagine why (on long buffers).

My suggestion is to have a queue of messages and a thread to send them. You might also want to check and see if there is a better protocol then TCP to take advantage of unordered messages. I think there is at least RDP (reliable datagram protocol) on Linux that might fit the bill.


Thanks!

Reply via email to