views:

195

answers:

3

Which of the following components of program state is shared across threads in a multithreaded process?

Register values, Heap Memory, Global Variables and Stack memory.


My suggestion; Only global variables, global variables are allocated on the heap? So Heap memory and Global Variables. Is this correct?

+4  A: 

Heap memory always.

Global variables depends on platform, usually they are shared.

Stack is thread-specific, as well as registers.

antti.huima
Shared on linux?
Algific
@data_jepp Yes, shared on linux. If you want to have thread-local "global" variables you can allocated thread-local storage using the pthread_* functions.
antti.huima
+1  A: 

It depends on the language and the thread implementation. For example, I don't think that even C lets you directly access the CPU registers, so it's rather moot whether, say, pthreads shares registers (which, for the record, I am fairly certain it does not). Also in C, global variables are not in fact allocated on the heap, though they may be in other languages.

The stack is more complicated. In C/pthreads, each thread has its own stack, but in other languages and threading models, the situation could be far more complicated simply because the underlying stack models may not be so simple.

Thom Smith
You can use inline assembly in C to access registers, but it starts to lose it's name of "Portable Assembly" then.
Carter Galle
A: 

stack : no

registers: no

heap: yes (if you have to choose y or n, the true answers is it depends)

globals: yes

pm100