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())


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


Member Function Documentation

void foreach ( const Functor &  fct)

Iterate all the local objects through a functor.

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

Get the local T object for the current calling thread.

the local T object.
T& operator* ( )

Get a reference on the shared object.

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

Get a pointer on the shared object.

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

Get the ith local T object

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

Get the number of local T objects

the number of local objects

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