>

Threads share the same address space as opposed to being sand-boxed like processes are.

The stack is just some memory in your application that has been specifically reserved and is used to hold things such as function parameters, local variables, and other function-related information.

Every thread has it’s own stack. This means that when a particular thread is executing, it will use it’s own specific stack to avoid trampling over other threads which might be idle or executing simultaneously in a multi-core system.

Remember that these stacks are all still inside the same address space which means that any thread can access the contents of another threads’ stack.

A simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <thread>

void Foo(int& i)
{
// if thread t is executing this function then j will sit inside thread t's stack
// if we call this function from the main thread then j will sit inside the main stack
int j = 456;

i++; // we can see i because threads share the same address space
}

int main()
{
int i = 123; // this will sit inside the main threads' stack

std::thread t(std::bind(&Foo, std::ref(i))); // we pass the address of i to our thread
t.join();

std::cout << i << '\n';
return 0;
}

An illustration:

As we can see, each thread has its own stack (which is just some part of the processes’ memory) which lives inside the same address space.

Static member variables are defined once per class and their “accessibility” from other parts of the program is defined in the C++11 standard §11 “Member access control”.

Static member variable initialization is performed in a thread safe manner before function main will be executed.

However, accessing a variable (class or instance) from different threads where at least one thread modifies the variable requires synchronization primitives like memory barriers, mutex, etc. Otherwise, your program has “undefined behavior”.

Technically saying, every thread has a separate stack, but it is not necessarily ‘private’. Other threads are usually allowed to access it.
The main reason each thread has its own stack is so that the thread can actually do something (like call a functions)

http://www.hoard.org/


##### Reference http://stackoverflow.com/questions/35466550/multiple-threads-inside-class-accessing-data