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
- two expression evaluations conflict if 1 of them modifies memory location (1.7) , other 1 reads or modifies same memory location.
- 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.
- 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
- 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, specializationatomic<integral>
provides additional atomic operations appropriate integral types. there shall specializationatomic<bool>
provides general atomic operations specified in 29.6.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
- 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
Post a Comment