Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/atomic/ATOMIC FLAG INIT"

From cppreference.com
< cpp‎ | atomic
m (Text replace - "/sidebar" to "/navbar")
(Added LWG issue #2159 DR.)
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{title|ATOMIC_FLAG_INIT}}
 
{{title|ATOMIC_FLAG_INIT}}
{{cpp/atomic/navbar}}
+
{{cpp/thread/navbar}}
{{ddcl list begin}}
+
{{ddcl|header=atomic|since=c++11|
{{ddcl list header | atomic}}
+
{{ddcl list item |
+
 
#define ATOMIC_FLAG_INIT /* implementation-defined */
 
#define ATOMIC_FLAG_INIT /* implementation-defined */
 
}}
 
}}
{{ddcl list end}}
 
  
Defines the expression which can be used to initialize {{c|std::atomic_flag}} to clear state. If the flag has static storage duration, this initialization is static.
+
Defines the initializer which can be used to initialize {{lc|std::atomic_flag}} to clear (false) state in the form {{c|1=std::atomic_flag v = ATOMIC_FLAG_INIT;}}. It is unspecified if it can be used with other initialization contexts.
 +
 
 +
If the flag has is a [[cpp/language/object#Subobjects|complete object]] with {{lsd|cpp/language/storage_duration#Static storage duration}}, this [[cpp/language/initialization#Static initialization|initialization is static]].
 +
 
 +
{{rev begin}}
 +
{{rev|until=c++20|
 +
This is the only way to initialize {{lc|std::atomic_flag}} to a definite value: the value held after any other initialization is unspecified.
 +
}}
 +
{{rev|since=c++20|
 +
This macro is no longer needed since default constructor of {{lc|std::atomic_flag}} initializes it to clear state. It is kept for the compatibility with C.
 +
}}
 +
{{rev end}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|  
+
|
| code=
+
|code=
 
#include <atomic>
 
#include <atomic>
  
std::atomic_flag static_flag = ATOMIC_FLAG_INIT;
+
std::atomic_flag static_flag = ATOMIC_FLAG_INIT; // static initialization,
 +
// guaranteed to be available during dynamic initialization of static objects.
  
 
int main()
 
int main()
 
{
 
{
     std::atomic_flag automatic_flag = ATOMIC_FLAG_INIT;
+
     std::atomic_flag automatic_flag = ATOMIC_FLAG_INIT; // guaranteed to work
    std::atomic_flag another_flag(ATOMIC_FLAG_INIT);
+
//    std::atomic_flag another_flag(ATOMIC_FLAG_INIT); // unspecified
 
}
 
}
| output=
+
|output=
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=2159|std=C++11|before=it was unclear whether {{tt|ATOMIC_FLAG_INIT}}<br>can be used with other initialization contexts|after=other usages are<br>not guaranteed}}
 +
{{dr list item|wg=lwg|dr=3659|std=C++20|before={{tt|ATOMIC_FLAG_INIT}} was deprecated, but needed in C on some platforms|after=it is undeprecated}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/atomic/dcl list atomic_flag}}
+
{{dsc inc|cpp/atomic/dsc atomic_flag}}
{{dcl list end}}
+
{{dsc see c|c/atomic/ATOMIC_FLAG_INIT}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 18:13, 17 October 2024

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
ATOMIC_FLAG_INIT
(C++11)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
Defined in header <atomic>
#define ATOMIC_FLAG_INIT /* implementation-defined */
(since C++11)

Defines the initializer which can be used to initialize std::atomic_flag to clear (false) state in the form std::atomic_flag v = ATOMIC_FLAG_INIT;. It is unspecified if it can be used with other initialization contexts.

If the flag has is a complete object with static storage duration, this initialization is static.

This is the only way to initialize std::atomic_flag to a definite value: the value held after any other initialization is unspecified.

(until C++20)

This macro is no longer needed since default constructor of std::atomic_flag initializes it to clear state. It is kept for the compatibility with C.

(since C++20)

[edit] Example

#include <atomic>
 
std::atomic_flag static_flag = ATOMIC_FLAG_INIT; // static initialization,
// guaranteed to be available during dynamic initialization of static objects.
 
int main()
{
    std::atomic_flag automatic_flag = ATOMIC_FLAG_INIT; // guaranteed to work
//    std::atomic_flag another_flag(ATOMIC_FLAG_INIT); // unspecified
}

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2159 C++11 it was unclear whether ATOMIC_FLAG_INIT
can be used with other initialization contexts
other usages are
not guaranteed
LWG 3659 C++20 ATOMIC_FLAG_INIT was deprecated, but needed in C on some platforms it is undeprecated

[edit] See also

the lock-free boolean atomic type
(class) [edit]
C documentation for ATOMIC_FLAG_INIT