concurrent_vector

A concurrent_vector<T> is a dynamically growable array of items of type T for which it is safe to simultaneously access elements in the vector while growing it. However, be careful not to let another task access an element that is under construction or is otherwise being modified. For safe concurrent growing, concurrent_vector has two methods for resizing that support common uses of dynamic arrays: grow_by and grow_to_at_least. The index of the first element is 0. The method grow_by(n) enables you to safely append n consecutive elements to a vector, and returns the index of the first appended element. Each element is initialized with T(). So for instance, Example 5-3 safely appends a C string to a shared vector.

Example 5-3. Concurrent vector

void Append( concurrent_vector<char>& vector, const char* string ) {
    size_t n = strlen(string)+1;
    memcpy( &vector[vector.grow_by(n)], string, n+1 );
}

The related method grow_to_at_least(n) grows a vector to size n if it is shorter. Con-current calls to grow_by and grow_to_at_least do not necessarily return in the order that elements are appended to the vector.

The size() method returns the number of elements in the vector, which may include elements that are still undergoing concurrent construction by grow_by and grow_to_ at_least. Also, it is safe to use iterators while the concurrent_vector is being grown, as long as the iterators never go past the current value of end(). However, the iterator may reference an element undergoing ...

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.