[For complete, up-to-date TBB information visit: http://www.ThreadingBuildingBlocks.org]

TbbRef (Ver. 20): 6.2 atomic<T> Template Class

6.2 atomic<T> Template Class

Summary

Template class for atomic operations.

Syntax

template<typename T> atomic;

Header

#include "tbb/atomic.h"

Description

An atomic<T> supports atomic read, write, fetch-and-add, fetch-and-store, and compare-and-swap. Type T may be an integral type or a pointer type. When T is a pointer type, arithmetic operations are interpreted as pointer arithmetic. For example, if x has type atomic<float*> and a float occupies four bytes, then ++x advances x by four bytes. The specialization atomic<void*> does not allow pointer arithmetic.

Some of the methods have template method variants that permit more selective memory fencing. On IA-32 and EM64T processors, they have the same effect as the non-templated variants. On Itanium processors, they may improve performance by allowing the memory subsystem more latitude on the orders of reads and write. Using them may improve performance. Table 22 shows the fencing for the non-template form.

Table 22: Memory Fences Implied by Non-Template Methods

Kind

Description

Default For

acquire

Operations after the fence never move over it.

read

release

Operations before the fence never move over it.

write

full

Operations on either side never move over it.

fetch_and_store,

fetch_and_add,

compare_and_swap

TIP: Template class atomic<T> does not have any non-trivial constructors, because such constructors could lead to accidental introduction of compiler temporaries that would subvert the purpose of atomic<T>. To create an atomic<T> with a specific value, default-construct it first, and afterwards assign a value to it.

Members

namespace tbb {

enum memory_semantics {

acquire,

release

};

struct atomic<T> {

typedef T value_type;

template<memory_semantics M>

value_type fetch_and_add( value_type addend );

value_type fetch_and_add( value_type addend );

template<memory_semantics M>

value_type fetch_and_increment();

value_type fetch_and_increment();

template<memory_semantics M>

value_type fetch_and_decrement();

value_type fetch_and_decrement();

template<memory_semantics M>

value_type compare_and_swap( value_type new_value,

value_type comparand );

value_type compare_and_swap( value_type new_value,

value_type comparand );

template<memory_semantics M>

value_type fetch_and_store( value_type new_value );

value_type fetch_and_store( value_type new_value );

operator value_type() const;

value_type operator=( value_type new_value );

value_type operator+=(value_type);

value_type operator-=(value_type);

value_type operator++();

value_type operator++(int);

value_type operator--();

value_type operator--(int);

};

}

6.2.1 enum memory_semantics

Description

Defines values used to select the template variants that permit more selective memory fencing (see Table 22).

6.2.7 value_type fetch_and_store( value_type new_value )

Effect

Let x be the value of *this. Atomically exchanges old value of x with new_value.

Returns

Original value of x.

[For complete, up-to-date TBB information visit: http://www.ThreadingBuildingBlocks.org]