1. The difference between atomic operations and synchronization mechanisms
- Atomic operations focus on data security;
- The synchronization mechanism focuses on thread safety.
2. What are the methods for implementing atomic operations?
When it comes to atomic operations, the first thing that comes to mind is atomic operations on integers, the atomic_t type, and there is a whole set of atomic operations APIs on integers that can be called. Since integers can be operated atomically, others should be able to, too. Combined with the definition of atomic operations, if you want to implement atomic operations on other types of structures, then add locks and put the parts that require atomic operations in the critical section.