Thread Local Storage and local method variables

Posted by miguel on Stack Overflow See other posts from Stack Overflow or by miguel
Published on 2010-05-06T08:39:30Z Indexed on 2010/05/06 8:48 UTC
Read the original article Hit count: 265

Filed under:
|

In c#, each thread has its own stack space.

If this is the case, why is the following code not thread-safe? (It is stated that this code is thread-safe on this post: Locking in C#

class Foo 
{ 
    private int count = 0; 
    public void TrySomething()     
    { 
        count++; 
    } 
} 

As count is an int (stack variable), surely this value would be isolated to an individual thread, on its own stack, and therefore thread-safe?

I am probably missing something here, but I dont understand what is actually in Thread Local Storage if not stack-based variables for the thread?

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading