Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definition of "atomic object"

Tags:

In standard jargon of C and C++, the phrase "atomic object" means "object of atomic type," does it not?

No standard will explicitly define every two-word phrase, so one does not fault the C and C++ standards for omitting explicit definition of this one. Nevertheless, when I read in the C++17 standard (draft here), sect. 4.7.1(4), that "all modifications to a particular atomic object M occur in some particular total order, called the modification order of M"—and when the standard repeatedly employs similar language to delimit ever more precise logic for concurrency—I would like to be sure that I am not inadvertently misunderstanding.

Do I assume correctly that the phrase "atomic object" means

  • object of atomic type?

The only plausible alternative I can imagine would be that the phrase instead meant

  • properly aligned object small enough that hardware could handle it atomically.

Which is it, please?

(Note: I tag this question both C and C++ because, when it comes to atomics, the two standards use almost identical language. For this reason, an expert in either language can answer as far as I know. If for some reason I am mistaken, then please remove the C tag and retain the C++.)

Reference: see also this question, for which my question is preliminary.

like image 616
thb Avatar asked Feb 26 '19 12:02

thb


People also ask

What is an atomic object C++?

Each instantiation and full specialization of the std::atomic template defines an atomic type. Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined.

What does atomic mean?

Generally, you can summarize atomic as "one at a time". For example, when accessing or mutating a property is atomic, it means that only one read or write operation can be performed at a time. If you have a program that reads a property atomically, this means that the property cannot change during this read operation.

What is atomic int in C?

In C, _Atomic is used as a type specifier. It is used to avoid the race condition if more than one thread attempts to update a variable simultaneously. It is defined in the stdatomic.

What are atomic operations in OS?

Atomic operations are sequences of instructions that guarantee atomic accesses and updates of shared single word variables. This means that atomic operations cannot protect accesses to complex data structures in the way that locks can, but they provide a very efficient way of serializing access to a single word.


1 Answers

In my view atomicity - strictly speaking - does not apply to types or objects, it applies to operations, i.e. you can say an operation is atomic or not.

By an "atomic object" we understand an object whose public interface exposes only atomic operations, i.e. all operations you can do with that object are atomic.

In C and C++ it may be that the concepts are defined the other way around: first define atomic objects and then define atomic operations in terms of atomic objects. It probably made sense for C and C++ to define it this way because the wording of the standard is primarily concerned with defining the language. However from a theoretical and abstract functionality perspective atomic operations are the main concern.

The C++ has the standard std::atomic<T> class template which fits the above descriptions.

like image 112
bolov Avatar answered Sep 17 '22 07:09

bolov