Does BeginSend <IList <ArraySegment ....) atomically send all ArraySegments?

The .NET Socket class provides the following method:

Socket.BeginSend Method (IList<ArraySegment<Byte>>, SocketFlags, AsyncCallback, Object) 

I have a BufferManager class that returns an ArraySegment<byte> specified, constant ArraySegment<byte> size. Now I have a message to send, let's say it has a size of 10 KB, so I can use 5 pre-allocated buffers to store this message and call Socket.BeginSend(IList<ArraySegment>>...) . Will this message be sent atomically, as it would be if I just used byte[] (that is, several parallel BeginSend operations will not mix messages on the remote site)?

Edit: clarify - I am using a TCP / IP socket and my BeginSend program calls from multiple threads at the same time. Let them say that we have two lists of array segments:
L1: a1 a2 a3
L2: b1 b2 b3
Now I call BeginSend (L1 ...) and BeginSend (L2 ...) from two threads simultaneously. I want to know if these two lists will not mix on the far side, and I will not read something like: a1 b1 b2 a2 b3 a3.

+6
source share
1 answer

All that BufferManager does is maintain a set of fixed memory blocks that you can use. BufferManager does not affect concurrency bytes, or how they are generally used at all, that was so. From the MSDN documentation:

You can use the BufferManager class to manage the buffer pool. The pool and its buffers are created when an instance of this class is created and destroyed when the buffer pool is restored by garbage collection. Each time you need to use a buffer, you take one from the pool, use it and return it to the pool when it is done. This process is much faster than creating and destroying a buffer every time you need to use it.

BufferManager is just a convenient way to avoid calls to the new [], and then wait until the GC destroys the blocks. When you finish sending these blocks, make sure you call BlockManager.ReturnBuffer, or these bytes will not be available for future messages, and will be delayed until BufferManager itself becomes GC'd. If he lives on BufferManager, and you send a lot of messages using these buffers, you are likely to lose huge amounts of memory.

+1
source

Source: https://habr.com/ru/post/891943/


All Articles