Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/atomic/atomic/wait"

From cppreference.com
m (fixed linebreaks)
m (more fixes)
Line 50: Line 50:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/atomic/{{{1|}}}/dsc notify_one}}
+
{{dsc inc | cpp/atomic/atomic/dsc notify_one | {{{1|}}}}}
{{dsc inc | cpp/atomic/{{{1|}}}/dsc notify_all}}
+
{{dsc inc | cpp/atomic/atomic/dsc notify_all | {{{1|}}}}}
 
{{#ifeq:{{{2|}}} | bool |
 
{{#ifeq:{{{2|}}} | bool |
 
{{dsc inc | cpp/atomic/dsc atomic_flag_notify_one}}
 
{{dsc inc | cpp/atomic/dsc atomic_flag_notify_one}}

Revision as of 08:49, 17 December 2019

Template:cpp/atomic//title Template:cpp/atomic//navbar

(1) (since C++20)
void wait(  old, std::memory_order = std::memory_order::seq_cst ) const noexcept;
void wait(  old, std::memory_order = std::memory_order::seq_cst ) const volatile noexcept;

Provides a means to check if the object's value is not the old one, and if it is the old one then blocks until its value changes to something else. This form of change-detection is often more efficient than simple polling or pure spinlocks.

Performs the following logical pseudo-code:
while(true) {
    if (load(order) != old) return;
    block_until_modified(this); // exposition only - implementation defined
}

The correlary functions to unblock are notify_one() or notify_all(), but see Notes section.

Contents

Preconditions

order is neither std::memory_order::release nor std::memory_order::acq_rel.

Parameters

old - the value to check the 's object no longer contains
order - the memory synchronization ordering for this operation: must not be std::memory_order::release nor std::memory_order::acq_rel

Return value

(none)

Notes

Due to the ABA problem, transient changes from old to something else and back to old might be missed, and not unblock.

Because the logical block_until_modified() pseudo-code is implementation defined, the functions may unblock and return due to reasons other than a notifying function being invoked.

Example

See also

notifies at least one thread waiting on the atomic object
(public member function of Template:cpp/atomic//title) [edit]
notifies all threads blocked waiting on the atomic object
(public member function of Template:cpp/atomic//title) [edit]
notifies a thread blocked in atomic_wait
(function template) [edit]
notifies all threads blocked in atomic_wait
(function template) [edit]