Explain the mechanics of multithreading, race conditions, and how to implement thread-safe data structures.