Is ArrayPool<T>.Rent(Int32) Method thread-safe?

Viewed 2219

I just found out about ArrayPool existence, but it's documentation is somewhat lacking.

I'd like to know if Rent(.) and Return(.) are thread-safe.

Edit: looks like I didn't notice the "Thread Safety" part of documentation; but reading some of the comments and answers I was relieved I wasn't the only one that didn't.

2 Answers

Update Comment from ta.speot.is

It literally says on Thread safety This class is thread-safe. All members may be used by multiple threads concurrently

Original

It doesn't say it on docs.microsoft.com, however there is a few references to the fact it is

Add a new System.Buffers namespace to the BCL for Resource Pooling

The Pool will be lightweight and thread-safe, allowing for fast Rent and Return calls from any thread within the process, along with minimal locking overhead, and 0 heap allocations on most Rent calls (exceptions to this will be called out below in the description of the Rent function).

Pooling large arrays with ArrayPool

Recommended: use the ArrayPool.Shared property, which returns a shared pool instance. It’s thread safe and all you need to remember is that it has a default max array length, equal to 2^20 (1024*1024 = 1 048 576).

.

Related