gatb.core-API-0.0.0
ThreadObject< T > Class Template Reference

Facility to share a common resource between several threads. More...

#include <System.hpp>

Public Member Functions

 ThreadObject (const T &object=T())
 
 ~ThreadObject ()
 
T & operator() ()
 
template<typename Functor >
void foreach (const Functor &fct)
 
T * operator-> ()
 
T & operator* ()
 
size_t size () const
 
T & operator[] (size_t idx)
 

Detailed Description

template<typename T>
class gatb::core::system::impl::ThreadObject< T >

Facility to share a common resource between several threads.

When using multithreading, one has to take care about reads/writes on a resource T by several threads at the same time.

There are two ways to cope with this:

  • using some synchronization mechanism (see ISynchronizer); each time the resource T is accessed, one has to lock the synchronizer before the access and unlock the synchronizer after the access
  • using local information in each thread instead of accessing the T shared resource; at the end of the threads, one has to use all the gathered local information to update the T resource in a serial way.

The ThreadObject class provides a generic way to follow the second approach. One provides the shared resource to it, then the resource is cloned in N threads and each cloned resource can be locally accessed within its thread without needing synchronization process. At the end of the threads, the ThreadObject provides a way to get each cloned local resource and so the user can update the original resource with the gathered information.

It is possible to give no initial resource to a ThreadObject instance; in such a case, a default original resource is created by using the default constructor of the type of the resource.

Sample of use:

// In order to ease this approach, we use a ThreadObject object. Such an object
// will provide local sums for each executing thread. After the iteration, it also
// provides a mean to get all the local sums and modify the global sum accordingly.
ThreadObject<int> sum;
// We iterate our range.
dispatcher.iterate (it, [&] (int i)
{
// We retrieve the local sum for the current executing thread with 'sum()'
// Note that this block instruction still doesn't refer explicit thread
// management; this is hidden through the () operator of the ThreadObject class.
sum() += i;
});
// We retrieve all local sums through the 'foreach' method.
// This loop is done 'nbCores' times.
sum.foreach ([&] (int localSum)
{
// Here, the *s expression represents the global integer; we add to it the
// current 'localSum' computed by one of the threads.
*sum += localSum;
});

Constructor & Destructor Documentation

ThreadObject ( const T &  object = T())
inline

Constructor

Parameters
[in]object: object to be shared by several threads. If none, default constructor of type T is used.
~ThreadObject ( )
inline

Destructor.

Member Function Documentation

void foreach ( const Functor &  fct)
inline

Iterate all the local objects through a functor.

Parameters
[in]fct: functor called for each local object. The functor must take a T or a const T& argument
T& operator() ( )
inline

Get the local T object for the current calling thread.

Returns
the local T object.
T& operator* ( )
inline

Get a reference on the shared object.

Returns
a reference on the shared object.
T* operator-> ( )
inline

Get a pointer on the shared object.

Returns
a pointer on the shared object.
T& operator[] ( size_t  idx)
inline

Get the ith local T object

Parameters
[in]idx: index of the local object to be returned
Returns
a reference on the wanted local object
size_t size ( ) const
inline

Get the number of local T objects

Returns
the number of local objects

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