dune-fem  2.8-git
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 
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 #ifdef USE_SMP_PARALLEL
61  assert( thread < value_.size() );
62 #endif
63  return value_
64 #ifdef USE_SMP_PARALLEL
65  [ thread ]
66 #endif
67  ;
68  }
69 
71  const ValueType& operator [] ( const unsigned int thread ) const {
72  assert( thread < size() );
73 #ifdef USE_SMP_PARALLEL
74  assert( thread < value_.size() );
75 #endif
76  return value_
77 #ifdef USE_SMP_PARALLEL
78  [ thread ]
79 #endif
80  ;
81  }
82  };
83 
84  } // end namespace Fem
85 
86 } // end namespace Dune
87 
88 
89 #endif
Definition: bindguard.hh:11
Definition: threadmanager.hh:45
static int maxThreads()
return maximal number of threads possbile in the current run
Definition: threadmanager.hh:59
static int thread()
return thread number
Definition: threadmanager.hh:65
ThreadSafeValue realizes thread safety for a given variable by creating an instance of this variable ...
Definition: threadsafevalue.hh:17
size_t size() const
return number of threads
Definition: threadsafevalue.hh:47
T ValueType
type of value to be thread safe
Definition: threadsafevalue.hh:25
ValueType & operator*()
return reference to thread private value
Definition: threadsafevalue.hh:50
ValueType & operator[](const unsigned int thread)
return reference to private value for given thread number
Definition: threadsafevalue.hh:58
ThreadSafeValue()
default constructor
Definition: threadsafevalue.hh:38
ThreadSafeValue(Args &&...args)
constructor initializing values for all threads given a init value
Definition: threadsafevalue.hh:29