Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | atomic
(created)
 
(fixed constness of arguments)
Line 6: Line 6:
 
{{dcl |  
 
{{dcl |  
 
template< class T >
 
template< class T >
void atomic_wait( std::atomic<T>* object,
+
void atomic_wait( const std::atomic<T>* object,
 
                   typename std::atomic<T>::value_type old );
 
                   typename std::atomic<T>::value_type old );
 
}}
 
}}
 
{{dcl |
 
{{dcl |
 
template< class T >
 
template< class T >
void atomic_wait( volatile std::atomic<T>* object,
+
void atomic_wait( const volatile std::atomic<T>* object,
 
                   typename std::atomic<T>::value_type old );
 
                   typename std::atomic<T>::value_type old );
 
}}
 
}}
Line 18: Line 18:
 
{{dcl |  
 
{{dcl |  
 
template< class T >
 
template< class T >
void atomic_wait_explicit( std::atomic<T>* object,  
+
void atomic_wait_explicit( const std::atomic<T>* object,  
 
                           typename std::atomic<T>::value_type old,  
 
                           typename std::atomic<T>::value_type old,  
 
                           std::memory_order order );
 
                           std::memory_order order );
Line 24: Line 24:
 
{{dcl |  
 
{{dcl |  
 
template< class T >
 
template< class T >
void atomic_wait_explicit( volatile std::atomic<T>* object,
+
void atomic_wait_explicit( const volatile std::atomic<T>* object,
 
                           typename std::atomic<T>::value_type old,  
 
                           typename std::atomic<T>::value_type old,  
 
                           std::memory_order order );
 
                           std::memory_order order );
Line 42: Line 42:
 
}}
 
}}
  
The correlary functions to unblock are {{lc|std::atomic_notify_one}} or {{lc|std::atomic_notify_all}}, but see Notes section.
+
The corresponding functions to unblock are {{lc|std::atomic_notify_one}} or {{lc|std::atomic_notify_all}}, but see Notes section.
 
+
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | object | pointer to the atomic object to modify}}
+
{{par | object | pointer to the atomic object to check and wait on}}
 
{{par | old | the value to check the atomic object no longer contains}}
 
{{par | old | the value to check the atomic object no longer contains}}
 
{{par | order | the memory synchronization ordering for this operation: must not be std::memory_order::release nor std::memory_order::acq_rel}}
 
{{par | order | the memory synchronization ordering for this operation: must not be std::memory_order::release nor std::memory_order::acq_rel}}
Line 54: Line 53:
 
===Return value===
 
===Return value===
 
(none)
 
(none)
 
  
 
===Notes===
 
===Notes===
Line 63: Line 61:
 
===Example===
 
===Example===
 
{{todo}}
 
{{todo}}
 
  
 
===See also===
 
===See also===

Revision as of 06:02, 17 December 2019

 
 
 
Defined in header <atomic>
(1) (since C++20)
template< class T >

void atomic_wait( const std::atomic<T>* object,

                  typename std::atomic<T>::value_type old );
template< class T >

void atomic_wait( const volatile std::atomic<T>* object,

                  typename std::atomic<T>::value_type old );
(2) (since C++20)
template< class T >

void atomic_wait_explicit( const std::atomic<T>* object,
                           typename std::atomic<T>::value_type old,

                           std::memory_order order );
template< class T >

void atomic_wait_explicit( const volatile std::atomic<T>* object,
                           typename std::atomic<T>::value_type old,

                           std::memory_order order );

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

1) Equivalent to: atomic_wait_explicit(object, old, std::memory_order_seq_cst).
2) Performs the following logical pseudo-code:
while(true) {
    if (object->load(order) != old) return;
    block_until_modified(object); // exposition only - implementation defined
}

The corresponding functions to unblock are std::atomic_notify_one or std::atomic_notify_all, but see Notes section.

Contents

Parameters

object - pointer to the atomic object to check and wait on
old - the value to check the atomic 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]