Do threads share registers, heap, stack and global variables?

  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    Threads
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
FrostScYthe
Messages
80
Reaction score
0
Hiya :),
1). I understand that threads are like processes except that they share the state information, and memory.
So I would think they share the values of the registers, the memory heap, and the stack, right? would they also share global variables?

2). Also, could a multithread solution that uses multiple user threads get better performance on a multiprocessor system than on a system with only 1 processor? I think it could get better performance if indeed we assign a thread to each processor, but I’m not sure whether the user level thread can allocate this processors or not?

Thanks in advance for any help

Frost.
 
Physics news on Phys.org
There are many different thread implementations, and they can actually differ in many respects. The most ubiquitous threading system is probably pthreads (POSIX threads), which is described here:

http://www.die.net/doc/linux/man/man7/pthreads.7.html

Note that pthreads share only the heap; they each have their own stack. Registers are not preserved across thread context switches, either.

The answer to your last question depends on the implementation. If the threads are implemented entirely in user space, they cannot be distributed across multiple processors. If the threads are implemented by the operating system kernel, on the other hand, they generally can be distributed across multiple processors.

- Warren