### Wait-free Ring Buffer Usage Example in C++ Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/usage_examples Demonstrates how to use the wait-free ring buffer. Shows examples of pushing an element into the buffer and popping an element from it, including checks for buffer full or empty conditions. ```cpp ringbuffer r; // try to insert an element if (r.push(42)) { /* succeeded */ } else { /* buffer full */ } // try to retrieve an element int value; if (r.pop(value)) { /* succeeded */ } else { /* buffer empty */ } ``` -------------------------------- ### Lock-free Multi-Producer Queue Usage Example in C++ Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/usage_examples Provides an example of how to use the lock-free multi-producer queue. It shows how to push elements onto the queue and then retrieve and process them in FIFO order, including memory management for the nodes. ```cpp lockfree_queue q; // insert elements q.push(42); q.push(2); // pop elements lockfree_queue::node * x = q.pop_all() while(x) { X * tmp = x; x = x->next; // process tmp->data, probably delete it afterwards delete tmp; } ``` -------------------------------- ### Structure with Padding Bits - rgb Example Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Illustrates a simple structure 'rgb' composed of three unsigned characters. This structure highlights potential issues with padding bits and atomic operations, especially when using boost::atomic_ref, as it may not support lock-free operations if the platform lacks atomic instructions for its size. ```cpp struct rgb { unsigned char r, g, b; // 3 bytes }; ``` -------------------------------- ### Atomic Reference Example with C++17 Deduction Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates the creation of an atomic_ref using C++17's template argument deduction. The 'object' variable is an integer, and 'ref' becomes an atomic_ref referencing it. ```cpp #include int object = 0; boost::atomic_ref ref(object); // C++17: ref is atomic_ref ``` -------------------------------- ### C++ Example: Custom Enum Bitwise Complement Operator Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates a user-defined bitwise complement operator for an enum class. This example shows how to handle bitwise operations for enumerations, noting that Boost.Atomic operations do not invoke user-defined operators directly. ```cpp enum class flags : std::uint32_t { none = 0u, one = 1u, two = 1u << 1u, four = 1u << 2u, all = (1u << 3u) - 1u }; flags operator~ (flags value) { // Only invert bits that are named in the enum } ``` -------------------------------- ### Atomic Reference - Incorrect Usage Example Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Shows an example of incorrect usage where an atomic_ref is created referencing a Derived object. This can lead to silent corruption as the reference may access memory outside the Base subobject, such as trailing padding. ```cpp #include struct Base { short a; char b; }; struct Derived : public Base { char c; }; Derived x; boost::atomic_ref ref(x); // bad: ref may corrupt x.c ``` -------------------------------- ### Boost Atomic Flag Initialization Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates static initialization of boost::atomic_flag, emphasizing compatibility with C++11 and the use of BOOST_ATOMIC_FLAG_INIT macro for initializing the flag to a clear state. ```cpp #include // Constant initialization similar to std::atomic_flag with ATOMIC_FLAG_INIT boost::atomic_flag flag; // Explicitly using the macro for interface parity with std::atomic_flag boost::atomic_flag flag_with_macro = BOOST_ATOMIC_FLAG_INIT; ``` -------------------------------- ### Include Boost.Atomic Capabilities Header Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Includes the necessary header file for Boost.Atomic capabilities. This is a prerequisite for using the feature testing macros. ```cpp #include ``` -------------------------------- ### Atomic Flag Basic Operations Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Illustrates fundamental atomic operations on boost::atomic_flag, including checking lock-free status, testing the flag state, setting the flag, and clearing the flag. ```cpp #include #include int main() { boost::atomic_flag atomicFlag; // Check if the atomic flag is lock-free if (atomicFlag.is_lock_free()) { std::cout << "Atomic flag is lock-free." << std::endl; } else { std::cout << "Atomic flag is not lock-free." << std::endl; } // Test the flag state (initially clear) bool isSet = atomicFlag.test(boost::memory_order_seq_cst); std::cout << "Flag is set: " << std::boolalpha << isSet << std::endl; // Set the flag and check the previous state bool wasSet = atomicFlag.test_and_set(boost::memory_order_seq_cst); std::cout << "Flag was set (before test_and_set): " << std::boolalpha << wasSet << std::endl; // Test the flag state again (now set) isSet = atomicFlag.test(boost::memory_order_seq_cst); std::cout << "Flag is set: " << std::boolalpha << isSet << std::endl; // Clear the flag atomicFlag.clear(boost::memory_order_seq_cst); std::cout << "Flag cleared." << std::endl; // Test the flag state one last time (now clear) isSet = atomicFlag.test(boost::memory_order_seq_cst); std::cout << "Flag is set: " << std::boolalpha << isSet << std::endl; return 0; } ``` -------------------------------- ### Atomic Flag Wait and Notify Operations Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates the usage of wait, wait_until, wait_for, notify_one, and notify_all operations for blocking and unblocking threads based on the atomic flag's state. ```cpp #include #include #include #include boost::atomic_flag shared_flag; void waiting_thread_func() { std::cout << "Waiting thread: Waiting for flag to be set..." << std::endl; // Wait until the flag is set (old value was false) shared_flag.wait(false, boost::memory_order_seq_cst); std::cout << "Waiting thread: Flag is now set!" << std::endl; // Clear the flag to potentially unblock other waiters shared_flag.clear(boost::memory_order_seq_cst); } int main() { shared_flag.clear(boost::memory_order_seq_cst); // Ensure flag is initially clear std::thread waiting_thread(waiting_thread_func); // Simulate some work before setting the flag std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "Main thread: Setting the flag..." << std::endl; shared_flag.test_and_set(boost::memory_order_seq_cst); // Notify potentially waiting threads shared_flag.notify_one(); // Or notify_all() if multiple waiters waiting_thread.join(); return 0; } ``` -------------------------------- ### Erroneous Consume Example (Computationally Independent) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/thread_coordination Highlights a common pitfall when using consume semantics where the subsequent operation (B) is not computationally dependent on the value loaded from the atomic variable. This can lead to erroneous behavior as the happens-before guarantee might not be correctly established. ```cpp atomic a(0); complex_data_structure data[2]; thread1: data[1] = ...; /* A */ a.store(1, memory_order::release); thread2: int index = a.load(memory_order::consume); complex_data_structure tmp; if (index == 0) tmp = data[0]; else tmp = data[1]; ``` -------------------------------- ### Include Boost.Atomic Header Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface This snippet demonstrates how to include the necessary header file for using Boost.Atomic functionalities. It's a prerequisite for any atomic operations in Boost. ```cpp #include ``` -------------------------------- ### Atomic Reference Construction and Properties Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Illustrates the syntax for constructing an atomic_ref from an external object and highlights key properties like required_alignment. Note that default construction is deleted. ```cpp #include #include struct MyType {}; // Example of required_alignment std::size_t alignment = boost::atomic_ref::required_alignment; // Creating an atomic reference to an existing object MyType obj; boost::atomic_ref ref(obj); // Copy construction boost::atomic_ref ref2(ref); // Note: atomic_ref() = delete; - not default constructible ``` -------------------------------- ### Basic Thread Conflict Example - Memory Reordering Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/thread_coordination Demonstrates a potential race condition where memory reordering by compilers, CPUs, or cache hierarchies can cause assertion failures. Thread1 writes to variables x and y, while Thread2 reads y and asserts x has been written. Without proper synchronization, Thread2 may not see Thread1's write to x even after seeing the write to y. ```cpp int x = 0, int y = 0; thread1: x = 1; y = 1; thread2: if (y == 1) { assert(x == 1); } ``` -------------------------------- ### Boost.Atomic Pointer Operations Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates atomic operations for pointer types using boost::atomic<_pointer_>, including fetch_add, fetch_sub, and custom Boost.Atomic extensions. These operations are essential for thread-safe manipulation of pointers. ```cpp #include #include int main() { boost::atomic ptr; int value = 5; ptr.store(&value); // fetch_add equivalent for pointers (not directly applicable, but conceptually similar for integer offsets) // For actual pointer arithmetic, use pointer arithmetic operators or C++20 std::atomic::fetch_add // Boost.Atomic extensions for pointer-like operations (demonstrative, actual use depends on T*) // Example for fetch_add (if T* were an integer offset, which it isn't directly) // auto prev_val = ptr.fetch_add(1, boost::atomic_load_explicit::memory_order_seq_cst); // Example for add_and_test (conceptual, requires a non-null return value interpretation) // bool test_result = ptr.add_and_test(1, boost::atomic_load_explicit::memory_order_seq_cst); int* current_ptr = ptr.load(); std::cout << "Current pointer value: " << current_ptr << std::endl; return 0; } ``` -------------------------------- ### Wait-free Ring Buffer Implementation in C++ Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/usage_examples Implements a wait-free ring buffer for efficient data transfer between a single producer and a single consumer thread without locks. It ensures operations complete in a constant number of steps, suitable for real-time systems. Relies on boost::atomic for thread-safe index management. ```cpp #include template class ringbuffer { public: ringbuffer() : head_(0), tail_(0) {} bool push(const T & value) { size_t head = head_.load(boost::memory_order::relaxed); size_t next_head = next(head); if (next_head == tail_.load(boost::memory_order::acquire)) return false; ring_[head] = value; head_.store(next_head, boost::memory_order::release); return true; } bool pop(T & value) { size_t tail = tail_.load(boost::memory_order::relaxed); if (tail == head_.load(boost::memory_order::acquire)) return false; value = ring_[tail]; tail_.store(next(tail), boost::memory_order::release); return true; } private: size_t next(size_t current) { return (current + 1) % Size; } T ring_[Size]; boost::atomic head_, tail_; }; ``` -------------------------------- ### Thread-Safe Singleton with Double-Checked Locking using Boost Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/usage_examples Ensures that at most one instance of a class is created. Implements the double-checked locking pattern for efficient access to an existing instance. Uses boost::atomic and boost::mutex for thread safety. ```cpp #include #include class X { public: static X * instance() { X * tmp = instance_.load(boost::memory_order::consume); if (!tmp) { boost::mutex::scoped_lock guard(instantiation_mutex); tmp = instance_.load(boost::memory_order::consume); if (!tmp) { tmp = new X; instance_.store(tmp, boost::memory_order::release); } } return tmp; } private: static boost::atomic instance_; static boost::mutex instantiation_mutex; }; boost::atomic X::instance_(0); ``` -------------------------------- ### Release-Acquire Memory Ordering with Boost.Atomic Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/thread_coordination Demonstrates the fundamental pattern for thread coordination using atomic variables with release and acquire semantics. Thread1 performs operation A then writes with release, while Thread2 reads with acquire then performs operation B, ensuring A happens-before B when Thread2 reads the value written by Thread1. ```cpp atomic a(0); thread1: ... /* A */ a.fetch_add(1, memory_order::release); thread2: int tmp = a.load(memory_order::acquire); if (tmp == 1) { ... /* B */ } else { ... /* C */ } ``` -------------------------------- ### Boost.Atomic Floating-Point Add Operation (Boost Extension) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates the `add` operation, a Boost.Atomic extension for floating-point types. It adds a specified value to the atomic variable and returns the result. ```cpp F add(F v, memory_order order) ``` -------------------------------- ### Happens-Before with Release/Consume Semantics Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/thread_coordination Illustrates thread coordination using release semantics on a store operation and consume semantics on a load operation. This establishes a happens-before relationship, ensuring that prior operations in the releasing thread are visible to dependent operations in the consuming thread, crucial for safe data access. ```cpp atomic a(0); complex_data_structure data[2]; thread1: data[1] = ...; /* A */ a.store(1, memory_order::release); thread2: int index = a.load(memory_order::consume); complex_data_structure tmp = data[index]; /* B */ ``` -------------------------------- ### Lock-free Multi-Producer Queue Implementation in C++ Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/usage_examples Implements a lock-free queue supporting multiple producers and a single consumer. Objects are enqueued and retrieved in First-In, First-Out (FIFO) order. Uses boost::atomic for thread-safe management of the queue's head pointer. ```cpp template class lockfree_queue { public: struct node { T data; node * next; }; void push(const T &data) { node * n = new node; n->data = data; node * stale_head = head_.load(boost::memory_order::relaxed); do { n->next = stale_head; } while (!head_.compare_exchange_weak(stale_head, n, boost::memory_order::release)); } node * pop_all(void) { T * last = pop_all_reverse(), * first = 0; while(last) { T * tmp = last; last = last->next; tmp->next = first; first = tmp; } return first; } lockfree_queue() : head_(0) {} // alternative interface if ordering is of no importance node * pop_all_reverse(void) { return head_.exchange(0, boost::memory_order::consume); } private: boost::atomic head_; }; ``` -------------------------------- ### Boost.Atomic Floating-Point Fetch Negate Operation (Boost Extension) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Illustrates the `fetch_negate` operation, a Boost.Atomic extension for floating-point types. It changes the sign of the stored value and returns the previous value. ```cpp F fetch_negate(memory_order order) ``` -------------------------------- ### Boost.Atomic Enumeration Bitwise XOR Operation Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Illustrates how to achieve a bitwise XOR operation similar to the user-defined operator~ using other atomic operations on a Boost.Atomic enumeration. It asserts the expected outcome. ```cpp atomic_flags.store(flags::none); // Achieve the behavior similar to the user-defined operator~ through other atomic operations assert(atomic_flags.bitwise_xor(flags::all) == flags::all); ``` -------------------------------- ### Reference Counting with Boost Intrusive Pointer and Atomic Operations Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/usage_examples Implements a reference counter for object destruction when no pointers reference it. Uses boost::intrusive_ptr and boost::atomic for thread-safe reference management. Ensures correct memory ordering for add and release operations. ```cpp #include #include class X { public: typedef boost::intrusive_ptr pointer; X() : refcount_(0) {} private: mutable boost::atomic refcount_; friend void intrusive_ptr_add_ref(const X * x) { x->refcount_.fetch_add(1, boost::memory_order::relaxed); } friend void intrusive_ptr_release(const X * x) { if (x->refcount_.fetch_sub(1, boost::memory_order::release) == 1) { boost::atomic_thread_fence(boost::memory_order::acquire); delete x; } } }; ``` -------------------------------- ### Partial Template Specialization with SFINAE for Clock Hierarchies Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Shows how to use template specialization with SFINAE (Substitution Failure Is Not An Error) and std::enable_if to provide partial specializations of posix_clock_traits that apply to multiple clock types sharing a common base class. This allows a single specialization to handle subsets of clock types that satisfy certain conditions. ```cpp #include #include namespace boost { namespace atomics { template struct posix_clock_traits::value>::type> { // POSIX clock identifier onto which MyClock maps static constexpr clockid_t clock_id = MyClock::clock_id; // Function that converts a MyClock time point to a POSIX timespec structure that corresponds to clock_id static timespec to_timespec(typename MyClock::time_point time_point) noexcept; }; } // namespace atomics } // namespace boost ``` -------------------------------- ### Check Native Wait/Notify Support with C++ Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Illustrates how to use Boost.Atomic macros to determine if native waiting and notifying operations are supported for atomic types. These macros indicate the availability of native operations for both non-IPC and IPC atomic types. ```cpp // Example for non-IPC atomic #if BOOST_ATOMIC_HAS_NATIVE_INT_WAIT_NOTIFY == 2 // Native wait/notify supported for non-IPC atomic #endif // Example for IPC atomic #if BOOST_ATOMIC_HAS_NATIVE_INT_IPC_WAIT_NOTIFY == 2 // Native wait/notify supported for IPC atomic #endif ``` -------------------------------- ### Atomic Fences with Release/Acquire Semantics Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/thread_coordination Demonstrates using atomic_thread_fence with release and acquire semantics to enforce ordering constraints between atomic operations, similar to memory barriers. This pattern can optimize synchronization by eliding expensive memory ordering when certain execution paths are not taken. ```cpp atomic a(0); thread1: /* A */ atomic_thread_fence(memory_order::release); a.fetch_add(1, memory_order::relaxed); thread2: int tmp = a.load(memory_order::relaxed); if (tmp == 1) { atomic_thread_fence(memory_order::acquire); /* B */ } else { /* C */ } ``` -------------------------------- ### Boost.Atomic Enumeration Bitwise Complement Operation Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates the bitwise complement operation on a Boost.Atomic enumeration, showcasing how it inverts all bits of the underlying integer type. It verifies the behavior using assertions. ```cpp atomic atomic_flags(flags::none); // bitwise_complement() operates on the value of the underlying type assert(atomic_flags.bitwise_complement() == (flags)0xFFFFFFFF); atomic_flags.store(flags::none); // Enforce behavior of the user-defined operator~ { flags old_val = atomic_flags.load(), new_val; do { new_val = ~old_val; } while (!atomic_flags.compare_exchange_weak(old_val, new_val)); assert(new_val == flags::all); } ``` -------------------------------- ### Boost Atomic Constructors Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Initializes a boost::atomic object. The default constructor performs value initialization (zero-initialization), differing from C++11's default initialization. The overloaded constructor allows initialization with a specific value. ```cpp boost::atomic atomic_var; boost::atomic atomic_var_init(10); ``` -------------------------------- ### Boost.Atomic Floating-Point Fetch Add Operation Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Shows the `fetch_add` operation for Boost.Atomic floating-point types, which adds a specified value to the atomic variable and returns the previous value. This operation is equivalent to `std::atomic::fetch_add`. ```cpp F fetch_add(F v, memory_order order) ``` -------------------------------- ### Boost.Atomic Floating-Point Opaque Add Operation (Boost Extension) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates the `opaque_add` operation, a Boost.Atomic extension for floating-point types. It adds a specified value to the atomic variable without returning the previous value, potentially offering performance benefits. ```cpp void opaque_add(F v, memory_order order) ``` -------------------------------- ### Boost.Atomic Fences for Thread and Signal Synchronization Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Implements fences for coordinating memory access between threads and within signal handlers. atomic_thread_fence synchronizes across threads, while atomic_signal_fence acts as a compiler barrier for signal handlers in the same thread. ```cpp #include void atomic_thread_fence(memory_order order); void atomic_signal_fence(memory_order order); ``` -------------------------------- ### Boost.Atomic IPC Types for Inter-Process Communication Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Provides atomic types for inter-process communication, ensuring lock-free and address-free operations, including support for waiting and notifying operations. It enforces lock-free requirements and offers compile-time detection of atomic instruction availability. ```cpp #include #include #include ``` -------------------------------- ### Boost Atomic Exchange Operation Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Atomically exchanges the current value with a new value, returning the original value before the exchange. ```cpp boost::atomic my_atomic(5); int old_value = my_atomic.exchange(10); // old_value is 5, my_atomic now holds 10 ``` -------------------------------- ### Boost.Atomic Floating-Point Subtract Operation (Boost Extension) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Illustrates the `sub` operation, a Boost.Atomic extension for floating-point types. It subtracts a specified value from the atomic variable and returns the result. ```cpp F sub(F v, memory_order order) ``` -------------------------------- ### Spinlock Implementation using Boost Atomic Operations Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/usage_examples A spinlock implementation to prevent concurrent access to shared data structures. Threads busy-wait when the lock is acquired. Uses boost::atomic for state management with acquire/release memory ordering. ```cpp #include class spinlock { private: typedef enum {Locked, Unlocked} LockState; boost::atomic state_; public: spinlock() : state_(Unlocked) {} void lock() { while (state_.exchange(Locked, boost::memory_order::acquire) == Locked) { /* busy-wait */ } } void unlock() { state_.store(Unlocked, boost::memory_order::release); } }; ``` -------------------------------- ### Sequential Consistency for Thread Coordination Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/thread_coordination Explains the use of seq_cst (sequentially consistent) memory order for thread coordination. Operations with seq_cst establish strong ordering guarantees, ensuring that either all operations from one thread happen before operations in another, or vice versa, regardless of which atomic variables are involved. ```cpp /* Example illustrating the concept, not a direct code snippet */ /* thread1 performs A, then seq_cst op, then B */ /* thread2 performs C, then seq_cst op, then D */ /* Guarantees: A happens-before D OR C happens-before B */ ``` -------------------------------- ### Boost.Atomic Integral Operations (Test and Set) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Demonstrates Boost.Atomic's 'and_test' operations for integral types. These operations perform a modification and return a boolean indicating whether the *result* of the operation is non-zero. This is useful for conditional logic based on atomic changes. Memory order is required. ```cpp template bool boost::atomic<_I_>::negate_and_test( memory_order order ); template bool boost::atomic<_I_>::add_and_test( _I_ v, memory_order order ); template bool boost::atomic<_I_>::sub_and_test( _I_ v, memory_order order ); template bool boost::atomic<_I_>::and_and_test( _I_ v, memory_order order ); template bool boost::atomic<_I_>::or_and_test( _I_ v, memory_order order ); template bool boost::atomic<_I_>::xor_and_test( _I_ v, memory_order order ); template bool boost::atomic<_I_>::complement_and_test( memory_order order ); ``` -------------------------------- ### Mutex-based Thread Synchronization Pattern Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/thread_coordination Shows how two threads can synchronize using a common mutex to enforce mutual exclusion. The happens-before relation guarantees that either operation A or B executes first, preventing concurrent execution and conflicts between critical sections. ```cpp mutex m; thread1: m.lock(); ... /* A */ m.unlock(); thread2: m.lock(); ... /* B */ m.unlock(); ``` -------------------------------- ### Boost.Atomic Integral Operations (Boost Extensions) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Showcases Boost.Atomic specific operations for integral types, including sign changes, bitwise complements, and operations that return the modified value. These extensions provide additional atomic manipulation capabilities beyond the standard set. Memory order is a required parameter. ```cpp template _I_ boost::atomic<_I_>::fetch_negate( memory_order order ); template _I_ boost::atomic<_I_>::fetch_complement( memory_order order ); template _I_ boost::atomic<_I_>::negate( memory_order order ); template _I_ boost::atomic<_I_>::add( _I_ v, memory_order order ); template _I_ boost::atomic<_I_>::sub( _I_ v, memory_order order ); template _I_ boost::atomic<_I_>::bitwise_and( _I_ v, memory_order order ); template _I_ boost::atomic<_I_>::bitwise_or( _I_ v, memory_order order ); template _I_ boost::atomic<_I_>::bitwise_xor( _I_ v, memory_order order ); template _I_ boost::atomic<_I_>::bitwise_complement( memory_order order ); ``` -------------------------------- ### Boost.Atomic Floating-Point Negate Operation (Boost Extension) Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Shows the `negate` operation, a Boost.Atomic extension for floating-point types. It changes the sign of the stored value and returns the new value. ```cpp F negate(memory_order order) ``` -------------------------------- ### Boost.Atomic Convenience Type Aliases Source: https://www.boost.org/doc/libs/latest/libs/atomic/doc/html/atomic/interface Defines shorthand type aliases for boost::atomic<_T_> to simplify the declaration of atomic variables for various fundamental and integral types. These aliases enhance code readability and reduce verbosity. ```cpp #include #include // Basic integral types using atomic_char = boost::atomic; using atomic_uchar = boost::atomic; using atomic_schar = boost::atomic; using atomic_ushort = boost::atomic; using atomic_short = boost::atomic; using atomic_uint = boost::atomic; using atomic_int = boost::atomic; using atomic_ulong = boost::atomic; using atomic_long = boost::atomic; using atomic_ullong = boost::atomic; using atomic_llong = boost::atomic; // Pointer and other types using atomic_address = boost::atomic; using atomic_bool = boost::atomic; using atomic_wchar_t = boost::atomic; // C++11 fixed-width integer types using atomic_uint8_t = boost::atomic; using atomic_int8_t = boost::atomic; using atomic_uint16_t = boost::atomic; using atomic_int16_t = boost::atomic; using atomic_uint32_t = boost::atomic; using atomic_int32_t = boost::atomic; using atomic_uint64_t = boost::atomic; using atomic_int64_t = boost::atomic; // C++11 integer types with least/fastest widths using atomic_int_least8_t = boost::atomic; using atomic_uint_least8_t = boost::atomic; // ... (other least/fastest width types omitted for brevity) // C++11 largest integer types using atomic_intmax_t = boost::atomic; using atomic_uintmax_t = boost::atomic; // Standard type aliases using atomic_size_t = boost::atomic; using atomic_ptrdiff_t = boost::atomic; using atomic_intptr_t = boost::atomic; using atomic_uintptr_t = boost::atomic; // Lock-free indicators (if defined) // using atomic_unsigned_lock_free = boost::atomic; // using atomic_signed_lock_free = boost::atomic; int main() { atomic_int counter; counter.store(10); return 0; } ```