But count isn't a stack variable; it's a member variable of a class. If you pass a reference to an instance of Foo
to another thread, you are in a non-thread-safe situation.
Thread Local Storage (TLS) is a wholly different concept than member variables. TLS associates values with the thread and not with a specific class. Think of it as global variables (or static member variables of classes), except that they are scoped to only be visible within a single thread; other threads can't even see then. They are not stored in the thread's stack, but in a special area that is private to the thread.
There still is room for trouble, though. If you store a reference to a Foo
in TLS in a thread it's initially invisible to other threads. If you then somehow copy that reference into another thread, you again are in a thread-unsafe situation, regardless of the fact that the first reference lives in TLS.