Is socket.send thread safe?

Jonathan JonathanILevi at gmail.com
Mon Mar 26 21:41:08 UTC 2018


On Monday, 26 March 2018 at 17:55:10 UTC, bauss wrote:
> 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...).
>>
>> Thanks!
>
> Define thread safe.
>
> It's safe in the way that the buffers each call to send will 
> have will be what you expect.
>
> Ex.
>
> thread1 sends [1,2,3] and thread2 sends [4,5,6]
>
> then you're guaranteed that what you receive would be [1,2,3] 
> and [4,5,6].
>
> What it isn't safe from would be race conditions.
>
> So you don't know if you get it like [1,2,3,4,5,6] or 
> [4,5,6,1,2,3].
>
> So if the order of the buffer matters then you should use a 
> mutex, but if the order doesn't matter then you don't need to.

You answered what I needed.

The order of receiving the messages is not a problem, merely that 
a message its self is not broken, ie: [4,1,2,5,3,6](This would 
not work!)

Thanks!


More information about the Digitalmars-d-learn mailing list