As we know, Linux threads share almost everything except stack. The reason is that the control flow of each thread may be different, so we have to use a portion of memory to store the return addresses, passing arguments and local variables.
There is a simple and interesting code from this article which can be used to find out what are inside a thread stack.
Let’s do some math…
The value of count of func1() is 524091 which means func1() is called 524091 times.
The size of a thread stack is 8MB which is shown as below.
# cat /proc/2/maps | grep stack
7fffdad0a000–7fffdb50a000 rw — 00000000 00:00 0 [stack]
8MB / 524091 = 16
which means 16 bytes data is pushed into stack when calling func1(). The data are rip (return address) and rbp (the base address of the previous frame), each of which is 8 bytes because my platform is 64-bits.