Is System.ServiceModel.Channels.BufferManager thread safe?

I create a buffer manager through the static member BufferManager.CreateBufferManager . This newly created BufferManager used by multiple threads.

Should I use lock with TakeBuffer() and ReturnBuffer() or is it thread safe by design?

+8
c # thread-safety
source share
2 answers

Internally, BufferManager.CreateBufferManager returns an instance of WrappingBufferManager that does not use the concurrency form, but wraps multiple SynchronizedPool<T> instances that use the internal lock on Take() in a new buffer. Therefore, judging by the simplicity of the WrappingBufferManager , we can safely assume that any lock on your part will be redundant, and that the returned class is actually thread safe.

+4
source share

I am having problems with the BufferManager. I created my own message encoder in WCF, and based on my observation, it did not always guarantee that a buffer manager is created for each call. Therefore, bytes are reused / returned by other threads, which leads to damage to my data.

So, to answer your question ... No, they are NOT . Since you are reusing the same instance, they are not guaranteed to be thread safe .

0
source share

All Articles