How to/best practices to write a thread safe code? Also how to identify if a code is thread safe or not?