Are C/C++ fundamental types atomic? -


are c/c++ fundamental types, int, double, etc., atomic, e.g. threadsafe?

are free data races; is, if 1 thread writes object of such type while thread reads it, behavior well-defined?

if not, depend on compiler or else?

no, fundamental data types (e.g., int, double) not atomic, see std::atomic.

instead can use std::atomic<int> or std::atomic<double>.

note: std::atomic introduced c++11 , understanding prior c++11, c++ standard didn't recognize existence of multithreading @ all.


as pointed out @josh, std::atomic_flag atomic boolean type. guaranteed lock-free, unlike std::atomic specializations.


the quoted documentation from: http://open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4567.pdf. i'm pretty sure standard not free , therefore isn't final/official version.

1.10 multi-threaded executions , data races

  1. two expression evaluations conflict if 1 of them modifies memory location (1.7) , other 1 reads or modifies same memory location.
  2. the library defines number of atomic operations (clause 29) , operations on mutexes (clause 30) specially identified synchronization operations. these operations play special role in making assignments in 1 thread visible another. synchronization operation on 1 or more memory locations either consume operation, acquire operation, release operation, or both acquire , release operation. synchronization operation without associated memory location fence , can either acquire fence, release fence, or both acquire , release fence. in addition, there relaxed atomic operations, not synchronization operations, , atomic read-modify-write operations, have special characteristics.


  1. two actions potentially concurrent if
    (23.1) — performed different threads, or
    (23.2) — unsequenced, , @ least 1 performed signal handler.
    execution of program contains data race if contains 2 potentially concurrent conflicting actions, @ least 1 of not atomic, , neither happens before other, except special case signal handlers described below. such data race results in undefined behavior.

29.5 atomic types

  1. there shall explicit specializations of atomic template integral types ``char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, char16_t, char32_t, wchar_t, , other types needed typedefs in header <cstdint>. each integral type integral, specialization atomic<integral> provides additional atomic operations appropriate integral types. there shall specialization atomic<bool> provides general atomic operations specified in 29.6.1..


  1. there shall pointer partial specializations of atomic class template. these specializations shall have standard layout, trivial default constructors, , trivial destructors. shall each support aggregate initialization syntax.

29.7 flag type , operations

  1. operations on object of type atomic_flag shall lock-free. [ note: hence operations should address-free. no other type requires lock-free operations, atomic_flag type minimum hardware-implemented type needed conform international standard. remaining types can emulated atomic_flag, though less ideal properties. — end note ]

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

post - imageshack API cURL -

dataset - MPAndroidchart returning no chart Data available -