dune-fem  2.4.1-rc
threadsafevalue.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_THREADSAFEVALUES_HH
2 #define DUNE_FEM_THREADSAFEVALUES_HH
3 
4 #include <vector>
6 
7 namespace Dune {
8 
9  namespace Fem {
10 
11 
15  template <class T>
17  {
18 #ifdef USE_SMP_PARALLEL
19  std::vector< T > value_;
20 #else
21  T value_;
22 #endif
23  public:
25  typedef T ValueType ;
26 
28  template< class ...Args >
29  ThreadSafeValue( Args&& ...args )
30 #ifdef USE_SMP_PARALLEL
31  : value_( ThreadManager::maxThreads(), ValueType( std::forward< Args >( args )... ) )
32 #else
33  : value_( std::forward< Args >( args )... )
34 #endif
35  {}
36 
39  : value_(
40 #ifdef USE_SMP_PARALLEL
41  ThreadManager::maxThreads()
42 #endif
43  )
44  {}
45 
47  size_t size() const { return ThreadManager::maxThreads(); }
48 
50  ValueType& operator * () { return this->operator[]( ThreadManager::thread() ); }
52  const ValueType& operator * () const { return this->operator[]( ThreadManager::thread() ); }
53 
54  operator const ValueType& () const { return this->operator[]( ThreadManager::thread() ); }
55  operator ValueType& () { return this->operator[]( ThreadManager::thread() ); }
56 
58  ValueType& operator [] ( const unsigned int thread ) {
59  assert( thread < size() );
60  return value_
61 #ifdef USE_SMP_PARALLEL
62  [ thread ]
63 #endif
64  ;
65  }
66 
68  const ValueType& operator [] ( const unsigned int thread ) const {
69  assert( thread < size() );
70  return value_
71 #ifdef USE_SMP_PARALLEL
72  [ thread ]
73 #endif
74  ;
75  }
76  };
77 
78  } // end namespace Fem
79 
80 } // end namespace Dune
81 
82 
83 #endif
ThreadSafeValue(Args &&...args)
constructor initializing values for all threads given a init value
Definition: threadsafevalue.hh:29
The ThreadManager wrapps basic shared memory functionality provided by OpenMP or pthreads such as thr...
Definition: threadmanager.hh:199
ValueType & operator[](const unsigned int thread)
return reference to private value for given thread number
Definition: threadsafevalue.hh:58
static int thread()
return thread number
Definition: threadmanager.hh:208
Definition: coordinate.hh:4
ValueType & operator*()
return reference to thread private value
Definition: threadsafevalue.hh:50
static int maxThreads()
return maximal number of threads possbile in the current run
Definition: threadmanager.hh:202
size_t size() const
return number of threads
Definition: threadsafevalue.hh:47
ThreadSafeValue realizes thread safety for a given variable by creating an instance of this variable ...
Definition: threadsafevalue.hh:16
T ValueType
type of value to be thread safe
Definition: threadsafevalue.hh:25
ThreadSafeValue()
default constructor
Definition: threadsafevalue.hh:38