How do you ensure that variables are safe when there are two threads involved
Interview Answers
Anonymous
Sep 27, 2012
Use c++ atomics types , use built-in assembly atomics, use mutex
Anonymous
Oct 14, 2012
If these variables form a critical section then that section must be protected by a lock, use java synchronized, a Lock like ReentrantLock or use a nonblocking algorithm by making use of the Atomic variables.
If the variables don't form a critical section, then you don't need locks, if possible use an object from Atomic package or make the variable volatile.