Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Atomic operations

An atomic operation is a set of operations that can be treated as a single operation for the purpose of being thread-safe. Atomic operations are thread-safe even without the protection of a critical section.

Atomic operations are similar to critical sections, but are often optimized to be faster than the equivalent critical section enter/leave operations (such as adding an integer constant to a word stored in a memory location).

During an atomic operation:

  • The thread must not be interrupted.
  • Intermediate states are invisible to other threads.

Every operation within an atomic operation must succeed for the system state to change.

Atomic operation rules

  • Use atomic operations to implement a short operation that needs to be thread-safe. With atomic operations, the write of the object can occur when the operation is committed.
  • AddRef and Release implementations require atomic add operations to make the object thread-safe.
  • Use atomic_compareOrAdd if doing a weak reference. (A weak reference occurs when an object keeps a pointer to another object without preventing the deletion of that object.)