Home > Net >  Can threads share their stacks? Or are they completely parallel to each other?
Can threads share their stacks? Or are they completely parallel to each other?

Time:06-13

One thread has to be created within an operation in another thread, does that mean that one thread exists within another thread's stack? Or are they completely parallel once initialized?

Or is this assumption completely wrong? Can they share anything other than the heap?

CodePudding user response:

No, systems threads of mainstream operating systems (Windows, Linux and Mac) have dedicated stacks. Stacks are not shared between threads. The stack of a thread is allocated when the operating system creates a thread (and also automatically freed when the thread ends). The creation of the stack is completely transparent from the user point-of-view.

Threads belonging to the same process share system resources like opened files, sockets (for networking operations), memory mapped areas (eg. to devices), etc. In fact, OS resources are generally bound to processes and no threads. A thread is basically few registers, a stack, a thread-local storage (and additional meta-informations like an ID and few attributes for example that are OS-dependent).

  • Related