Difference between stack and call stack in multithreading?

While reading the answer that all materials share streams, I came across the term “call stack”. While I know that threads have their own stack, which they do not share with other threads, I do not understand what the call stack will mean in relation to the thread. I saw some answers, but they were not very clear. Please clarify what the call stack is and how it differs from the stack in the context of multitasking. Relations

+5
source share
3 answers

Clarify what the call stack means and how it differs from the stack in the context of multitasking.

The difference is subtle, but as I understand it. Often people use them somewhat interchangeably, but the call stack is just a data structure. It describes a stack of function calls along with various associated states, such as local variable values, return states, etc.

The stack is also a data structure, but ultimately it is a memory allocator. It combines the memory allocated for a thread, which is used for things like the call stack, with the very simple, constant, symmetric push and pop style of allocating and freeing memory.

You can think of it as, roughly speaking, the connection between std::vector and std::allocator . std::vector is strictly a data structure. std::allocator allocates memory for it (which is usually associated with the data structure behind the hood, but the data structure is used exclusively for memory management). But std::vector does not have to use std::allocator .

Conceptually, the call stack should not actually use the stack to allocate memory. In practice, it would be difficult to find a compiler that ever does this. For example, a call stack may actually use a heap instead of a stack. In fact, this may require linear memory allocation of memory each time he just wants to press an argument to call a function. That would be terrible, but it would not be incompatible with the concept of the call stack.

Typically, call stacks use a thread-local stack to allocate memory because it is practical, efficient, consistent with the expected nature of LIFO allocation / deallocation, and allows each thread to have its own memory space (alleviating bottlenecks associated with accessing shared memory).

+2
source

The call stack is a stack data structure that stores information about the active routines of a computer program.

Where thread stacks are what constitutes a private thread stack, and you know about that.

If the thread performs the function, then the current call stack function will be stored in the thread stack .

These two things are essentially the same. They are stack data structures.

+1
source

Read wikipages on call stack and multithreading .

In pure theory, a C implementation may not even use any stack. In practice, every compiled C implementation I’ve heard of uses a call stack, which is a processor stack (some processors, perhaps Itanium IA-64 , sort — have two machine stacks) when there is one (AFAIK, IBM z Series mainframe doesn’t have no hardware stack, this is the usual use of some registers). Therefore, for most processors and ABIs (ARM, x86, x86-64, ...), the call stack is a stack, and each thread has its own.

0
source

Source: https://habr.com/ru/post/1236312/


All Articles