Using Parallel Extensions with ThreadStatic attribute. Could it leak memory?

Posted by the-locster on Stack Overflow See other posts from Stack Overflow or by the-locster
Published on 2010-06-12T17:09:07Z Indexed on 2010/06/12 17:12 UTC
Read the original article Hit count: 394

I'm using Parallel Extensions fairly heavily and I've just now encountered a case where using thread locla storrage might be sensible to allow re-use of objects by worker threads. As such I was lookign at the ThreadStatic attribute which marks a static field/variable as having a unique value per thread.

It seems to me that it would be unwise to use PE with the ThreadStatic attribute without any guarantee of thread re-use by PE. That is, if threads are created and destroyed to some degree would the variables (and thus objects they point to) remain in thread local storage for some indeterminate amount of time, thus causing a memory leak? Or perhaps the thread storage is tied to the threads and disposed of when the threads are disposed? But then you still potentially have threads in a pool that are longed lived and that accumulate thread local storage from various pieces of code the threads are used for.

Is there a better approach to obtaining thread local storage with PE?

Thankyou.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about parallel-extensions