Interview Question
Software Engineer / DevelopersCountry: India
@tejaswibm: It is just the stack that is not shared between threads. Heap, data and code are share between the threads.
Internally, it's possible for different threads to allocate on different heaps to avoid the need for synchronization. In that situation, thread-specific heaps may be copied over to a shared heap periodically. These details are highly language, compiler, and runtime-specific, though.
The answer above should be more than sufficient for your typical interviewer.
Also there's 'thread specific storage' which is either a compiler extension or, now, in C++11, baked into the language I believe, which is not shared. But, what eugene says goes for this too.
In some languages, like Java, you can extend the Thread class and put whatever instance variables you want in the extending class. When you then instantiate this class, you'll create threads that each have their own copies of these instance variables. This kind of thing is useful when you need each thread to have some kind of functionality that involves keeping track of internal state, but the desired functionality logically has no dependency between threads. An example would be a random number generator. Using a per-thread random number generator will remove any kind of lock contention that might otherwise have existed.
No, threads don't share program counters. How could they? A program counter represents which instruction you're currently executing, which is related to what line of code you're currently executing. Since the whole point of threads is that they allow you to execute different parts of the program at the same time, each thread must have its own program counter.
Shared:
- jzhu May 20, 2012- code
- data
- heap memory and memory address
- memory management information (base/relocation & limit registers, page tables
- process state (new, ready, running, waiting, halted, etc)
- I/O status information (opened file descriptors, etc)
- signals
- environment variables
Not shared:
- registers
- stack
- thread specific data