What will happen if multiple threads accessing the same resource?
Code that is safe to call by multiple threads simultaneously is called thread safe. If a piece of code is thread safe, then it contains no race conditions. Race condition only occur when multiple threads update shared resources.
What problem can occur when two threads share memory?
Thread Interference Error. When multiple threads share the same memory, there is a chance that two or more different threads performing different operations on the same data interleave with each other and create inconsistent data in the memory.
How are multiple threads inside a class accessing data?
The thread started during construction of c1 will have access to c1’s members only and the thread started in c2 will have access to c2’s members only. If you are talking about several threads within one class (not obvious from your code), then each thread has a copy of this which is just a pointer to your MyClass object.
Why do threads share the same address space?
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.
When do all the threads get the same variable?
When an object of this class is created, in main () for example, all the threads can access the same member variables. If every thread gets its own stack, why doesn’t each thread get a copy of the member variables rather than access to the same variable.
When do you never need to synchronise a thread?
If nothing can change, you never need to synchronise, because there will never be any chance that more than one thread will try to write at once. You can even combine them, if one process creates immutable shared memory data sets, it can use messages to pass pointers to consumers of that data.