Wildmeshing Toolkit
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Attributes | List of all members
wmtk::threading::collector< T > Class Template Reference

#include <collector.hpp>

Public Types

using value_type = T
 
using iterator = typename std::vector< T >::iterator
 
using const_iterator = typename std::vector< T >::const_iterator
 
using reference = typename std::vector< T >::reference
 
using const_reference = typename std::vector< T >::const_reference
 

Public Member Functions

 collector (std::size_t n)
 
 collector (std::size_t n, const T &value)
 
 collector (const collector &o)
 
collectoroperator= (const collector &o)
 
void push_back (const T &v)
 Push an element to the end of the collector. This function is thread-safe.
 
void push_back (T &&v)
 
template<typename... Args>
void emplace_back (Args &&... args)
 Emplace an element at the end of the collector. This function is thread-safe.
 
void append (const std::vector< T > &v)
 Append a vector of elements to the end of the collector. This function is thread-safe.
 
void resize (std::size_t n)
 Resize the collector to contain n elements. This function is thread-safe.
 
void resize (std::size_t n, const T &value)
 
void clear ()
 Clear the collector. This function is thread-safe.
 
void reserve (std::size_t n)
 Reserve space in the collector. This function is thread-safe.
 
const std::vector< T > & data () const
 Get the underlying data vector. This function is NOT thread-safe.
 
const_reference operator[] (std::size_t i) const
 operator[] is not thread-safe for concurrent writes, but is safe for concurrent reads after all writes are done. Only const reference is provided to prevent accidental writes.
 
std::size_t size () const
 Get the number of elements in the collector. This function is NOT thread-safe.
 
bool empty () const
 Check if the collector is empty. This function is NOT thread-safe.
 
const_iterator begin () const
 
const_iterator end () const
 

Private Attributes

std::vector< T > m_data
 
std::mutex m_mutex
 

Detailed Description

template<typename T>
class wmtk::threading::collector< T >

collector replaces tbb::concurrent_vector for the "append from a parallel loop, read afterwards" use cases. Writes with push_back and emplace_back are thread-safe, but reads with operator[] are not thread-safe! Reads should only be done after all writes are complete.

Member Function Documentation

◆ append()

template<typename T >
void wmtk::threading::collector< T >::append ( const std::vector< T > &  v)
inline

Append a vector of elements to the end of the collector. This function is thread-safe.

Parameters
vThe vector of elements to append.

◆ data()

template<typename T >
const std::vector< T > & wmtk::threading::collector< T >::data ( ) const
inline

Get the underlying data vector. This function is NOT thread-safe.

Returns
The underlying data vector.

◆ emplace_back()

template<typename T >
template<typename... Args>
void wmtk::threading::collector< T >::emplace_back ( Args &&...  args)
inline

Emplace an element at the end of the collector. This function is thread-safe.

Parameters
argsThe arguments to forward to the constructor of T.

◆ empty()

template<typename T >
bool wmtk::threading::collector< T >::empty ( ) const
inline

Check if the collector is empty. This function is NOT thread-safe.

Returns
True if the collector is empty, false otherwise.

◆ operator[]()

template<typename T >
const_reference wmtk::threading::collector< T >::operator[] ( std::size_t  i) const
inline

operator[] is not thread-safe for concurrent writes, but is safe for concurrent reads after all writes are done. Only const reference is provided to prevent accidental writes.

Parameters
iindex
Returns
const reference to the element at index i

◆ push_back()

template<typename T >
void wmtk::threading::collector< T >::push_back ( const T &  v)
inline

Push an element to the end of the collector. This function is thread-safe.

Parameters
vThe element to push.

◆ reserve()

template<typename T >
void wmtk::threading::collector< T >::reserve ( std::size_t  n)
inline

Reserve space in the collector. This function is thread-safe.

Parameters
nThe number of elements to reserve space for.

◆ resize()

template<typename T >
void wmtk::threading::collector< T >::resize ( std::size_t  n)
inline

Resize the collector to contain n elements. This function is thread-safe.

Parameters
nThe new size of the collector.

◆ size()

template<typename T >
std::size_t wmtk::threading::collector< T >::size ( ) const
inline

Get the number of elements in the collector. This function is NOT thread-safe.

Returns
The number of elements in the collector.

The documentation for this class was generated from the following file: