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

TbbRef (Ver. 20): 4.3 concurrent_vector

4.3 concurrent_vector

Summary

Template class for vector that can be concurrently grown and accessed.

Syntax

template<typename T> class concurrent_vector;

Header

#include "tbb/concurrent_vector.h"

Description

A concurrent_vector is a dynamically growable array for which it is safe to simultaneously access elements in the vector while growing it. The index of the first element is 0.

Members

namespace tbb {

template<typename T>

class concurrent_vector {

public:

typedef size_t size_type;

typedef T value_type;

typedef ptrdiff_t difference_type;

typedef T& reference;

typedef const T& const_reference;

// whole vector operations

concurrent_vector() {}

concurrent_vector( const concurrent_vector& );

concurrent_vector& operator=( const concurrent_vector&);

~concurrent_vector();

void clear();

// concurrent operations

size_type grow_by( size_type delta );

void grow_to_at_least( size_type new_size );

size_type push_back( const_reference value );

reference operator[]( size_type index );

const_reference operator[]( size_type index ) const;

// parallel iteration

typedef implementation-defined iterator;

typedef implementation-defined const_iterator;

typedef generic_range_type<iterator> range_type;

typedef generic_range_type<const_iterator> const_range_type;

range_type range( size_t grainsize );

const_range_type range( size_t grainsize ) const;

// capacity

size_type size() const;

bool empty() const;

size_type capacity() const;

void reserve( size_type n );

size_type max_size() const;

// STL support

iterator begin();

iterator end();

const_iterator begin() const;

const_iterator end() const;

typedef implementation-defined reverse_iterator;

typedef implementation-defined const_reverse_iterator;

iterator rbegin();

iterator rend();

const_iterator rbegin() const;

const_iterator rend() const;

};

}

4.3.1 Whole Vector Operations

These operations are not thread safe on the same instance.

4.3.1.1 concurrent_vector()

Effects

Construct empty vector.

4.3.1.2 concurrent_vector( const concurrent_vector& src )

Effects

Construct copy of src.

4.3.1.3 concurrent_vector& operator=( const concurrent_vector& src )

Effects

Assign contents of src to *this.

Returns

Reference to left hand side.

4.3.1.4 ~concurrent_vector()

Effects

Erase all elements and destroy the vector.

4.3.1.5 void clear()

Effects

Erase all elements. Afterwards, size()==0.

4.3.2 Concurrent Operations

The methods described in this section safely execute on the same instance of a concurrent_vector<T>.

4.3.2.1 size_type grow_by( size_type delta )

Effects

Atomically append delta elements to the end of the vector. The new elements are initialized with T(), where T is the value_type of the vector.

Returns

Old size of the vector. If it returns k, then the new elements are at the half-open index range [k..k+delta).

4.3.2.2 void grow_to_at_least( size_type n )

Effects

Grow the vector until it has at least n elements. The new elements are initialized with T(), where T is the value_type of the vector.

4.3.2.3 size_t push_back( const_reference value );

Effects

Atomically append copy of value to the end of the vector.

Returns

Index of the copy.

4.3.2.4 reference operator[]( size_type index )

Returns

Reference to element with the specified index.

4.3.2.5 const_reference operator[]( size_type index ) const;

Returns

Const reference to element with the specified index.

4.3.3 Parallel Iteration

Types const_range_type and range_type model the Range concept ( 3.2 ) and provide methods to access the bounds of the range as shown in Table 15 . The types differ only in that the bounds for a const_range_type are of type const_iterator, whereas the bounds for a range_type are of type iterator.

Use the range types in conjunction with parallel_for ( 3.4), parallel_reduce ( 3.5), and parallel_scan ( . 3.6) to iterate over pairs in a concurrent_vector.

Table 17: Concept for concurrent_vector Range R

Pseudo-Signature

Semantics

R::iterator R::begin() const

First item in range

R::iterator R::end() const

One past last item in range

4.3.3.1 range_type range( size_t grainsize )

Returns

Range over entire concurrent_vector that permits read-write access.

4.3.3.2 const_range_type range( size_t grainsize ) const

Returns

Range over entire concurrent_vector that permits read-only access.

4.3.4 Capacity

4.3.4.1 size_type size() const

Returns

Number of elements in the vector. The result may include elements that are under construction by concurrent calls to methods grow_by ( 4.3.2.1) or grow_to_at_least ( 4.3.2.2).

4.3.4.2 bool empty() const

Returns

size()==0.

4.3.4.3 size_type capacity() const

Returns

Maximum size to which vector can grow without having to allocate more memory.

NOTE: Unlike an STL vector, a concurrent_vector does not move existing elements if it has to allocate more memory.

4.3.4.4 void reserve( size_type n )

Returns

Reserve space for at least n elements.

Throws

std::length_error if n>max_size().

4.3.4.5 size_type max_size() const

Returns

Highest size vector that might be representable.

4.3.5 Iterators

Template class concurrent_vector<T> supports random access iterators as defined in Section 24.1.4 of the ISO C++ Standard. Unlike a std::vector, the iterators are not raw pointers. A concurrent_vector<T> meets the reversible container requirements in Table 66 of the ISO C++ Standard.

4.3.5.1 iterator begin()

Returns

iterator pointing to beginning of the vector.

4.3.5.2 iterator end()

Returns

iterator pointing to end of the vector.

4.3.5.3 const_iterator begin() const

Returns

const_iterator with pointing to beginning of the vector.

4.3.5.4 const_iterator end() const

Returns

const_iterator pointing to end of the vector.

4.3.5.5 iterator rbegin()

Returns

const_reverse_iterator(end())

4.3.5.6 iterator rend()

Returns

const_reverse_iterator(begin())

4.3.5.7 const_reverse_iterator rbegin() const

Returns

const_reverse_iterator(end())

4.3.5.8 const_ reverse_iterator rend() const

Returns

const_reverse_iterator(begin())

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