You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

A consistent locking policy guarantees that multiple threads cannot simultaneously access or modify shared data. Atomic variables eliminate the need for locks by guaranteeing thread-safety when certain operations are performed on them. The thread-safe operations on atomic variables are defined specifically in C11, sections 7.17.7 and 7.17.8. While they may be combined, combined operations do not provide the thread-safety provided by individual atomic operations.

Every time an atomic variable appears on the left hand side of an assignment operator, including a compound assignment operator such as {{*=}}, an atomic write is performed on the variable. Usage of the {{++}} or {{--}} operators on an atomic variable constitute an atomic read-and-write operation and are thus thread-safe. Any reference of an atomic variable anywhere else in an expression indicates a distinct atomic read on the variable.

If the same atomic variable appears twice in an expression, then two atomic reads, or an atomic read and an atomic write, are required. Such a pair of atomic operations is not thread-safe and is therefore not permitted.

  • No labels