.. _libcudacxx-extended-api-synchronization-atomic: cuda::atomic ================ .. toctree:: :hidden: :maxdepth: 1 atomic/atomic_thread_fence atomic/fetch_max atomic/fetch_min Defined in header ````: .. code:: cuda template class cuda::atomic; The class template ``cuda::atomic`` is an extended form of `cuda::std::atomic `_ that takes an additional :ref:`cuda::thread_scope ` argument, defaulted to ``cuda::std::thread_scope_system``. It has the same interface and semantics as `cuda::std::atomic `_, with the following additional operations. .. list-table:: :widths: 25 75 :header-rows: 0 * - :ref:`cuda::atomic_thread_fence ` - Memory order and scope dependent fence synchronization primitive. * - :ref:`cuda::atomic::fetch_min ` - Atomically find the minimum of the stored value and a provided value. * - :ref:`cuda::atomic::fetch_max ` - Atomically find the maximum of the stored value and a provided value. Concurrency Restrictions ------------------------ An object of type ``cuda::atomic`` or `cuda::std::atomic `_ shall not be accessed concurrently by CPU and GPU threads unless: - it is in unified memory and the `concurrentManagedAccess property `_ is 1, or - it is in CPU memory and the `hostNativeAtomicSupported property `_ is 1. Note, for objects of scopes other than ``cuda::thread_scope_system`` this is a data-race, and therefore also prohibited regardless of memory characteristics. Under CUDA Compute Capability 6 (Pascal), an object of type ``atomic`` may not be used: - with automatic storage duration, or - if ``is_always_lock_free()`` is ``false``. Under CUDA Compute Capability prior to 6 (Pascal), objects of type ``cuda::atomic`` or `cuda::std::atomic `_ may not be used. Implementation-Defined Behavior ------------------------------- For each type ``T`` and :ref:`cuda::thread_scope ` ``S``, the value of ``cuda::atomic::is_always_lock_free()`` is as follows: .. list-table:: :widths: 25 25 50 :header-rows: 0 * - Type ``T`` - :ref:`cuda::thread_scope ` ``S`` - ``cuda::atomic::is_always_lock_free()`` * - Any valid type - Any thread scope - ``sizeof(T) <= 8`` Example ------- .. code:: cuda #include __global__ void example_kernel() { // This atomic is suitable for all threads in the system. cuda::atomic a; // This atomic has the same type as the previous one (`a`). cuda::atomic b; // This atomic is suitable for all threads on the current processor (e.g. GPU). cuda::atomic c; // This atomic is suitable for threads in the same thread block. cuda::atomic d; } `See it on Godbolt `_