Logo ROOT   6.16/01
Reference Guide
List of all members | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
ROOT::TThreadedObject< T > Class Template Reference

template<class T>
class ROOT::TThreadedObject< T >

A wrapper to make object instances thread private, lazily.

Template Parameters
TClass of the object to be made thread private (e.g. TH1F)

A wrapper which makes objects thread private. The methods of the underlying object can be invoked via the the arrow operator. The object is created in a specific thread lazily, i.e. upon invocation of one of its methods. The correct object pointer from within a particular thread can be accessed with the overloaded arrow operator or with the Get method. In case an elaborate thread management is in place, e.g. in presence of stream of operations or "processing slots", it is also possible to manually select the correct object pointer explicitly. The default size of the threaded objects is 64. This size can be extended manually via the fgMaxSlots parameter. The size of individual instances is automatically extended if the size of the implicit MT pool is bigger than 64.

Definition at line 153 of file TThreadedObject.hxx.

Public Member Functions

template<class ... ARGS>
 TThreadedObject (ARGS &&... args)
 Construct the TThreaded object and the "model" of the thread private objects. More...
 
 TThreadedObject (const TThreadedObject &)=delete
 
std::shared_ptr< T > Get ()
 Access the pointer corresponding to the current slot. More...
 
std::shared_ptr< T > GetAtSlot (unsigned i)
 Access a particular processing slot. More...
 
T * GetAtSlotRaw (unsigned i) const
 Access a particular slot which corresponds to a single thread. More...
 
std::shared_ptr< T > GetAtSlotUnchecked (unsigned i) const
 Access a particular slot which corresponds to a single thread. More...
 
std::shared_ptr< T > Merge (TThreadedObjectUtils::MergeFunctionType< T > mergeFunction=TThreadedObjectUtils::MergeTObjects< T >)
 Merge all the thread private objects. More...
 
T * operator-> ()
 Access the wrapped object and allow to call its methods. More...
 
void SetAtSlot (unsigned i, std::shared_ptr< T > v)
 Set the value of a particular slot. More...
 
std::unique_ptr< T > SnapshotMerge (TThreadedObjectUtils::MergeFunctionType< T > mergeFunction=TThreadedObjectUtils::MergeTObjects< T >)
 Merge all the thread private objects. More...
 

Static Public Attributes

static unsigned fgMaxSlots = 64
 The maximum number of processing slots (distinct threads) which the instances can manage. More...
 

Private Member Functions

unsigned GetThisSlotNumber ()
 Get the slot number for this threadID. More...
 

Private Attributes

unsigned fCurrMaxSlotIndex = 0
 The maximum slot index. More...
 
std::vector< TDirectory * > fDirectories
 A TDirectory per thread is kept. More...
 
bool fIsMerged = false
 Remember if the objects have been merged already. More...
 
unsigned fMaxSlots
 The size of the instance. More...
 
std::unique_ptr< T > fModel
 Use to store a "model" of the object. More...
 
std::vector< std::shared_ptr< T > > fObjPointers
 A pointer per thread is kept. More...
 
std::map< std::thread::id, unsigned > fThrIDSlotMap
 A mapping between the thread IDs and the slots. More...
 
ROOT::TSpinMutex fThrIDSlotMutex
 Mutex to protect the ID-slot map access. More...
 

#include <ROOT/TThreadedObject.hxx>

Constructor & Destructor Documentation

◆ TThreadedObject() [1/2]

template<class T >
ROOT::TThreadedObject< T >::TThreadedObject ( const TThreadedObject< T > &  )
delete

◆ TThreadedObject() [2/2]

template<class T >
template<class ... ARGS>
ROOT::TThreadedObject< T >::TThreadedObject ( ARGS &&...  args)
inline

Construct the TThreaded object and the "model" of the thread private objects.

Template Parameters
ARGSArguments of the constructor of T

Definition at line 161 of file TThreadedObject.hxx.

Member Function Documentation

◆ Get()

template<class T >
std::shared_ptr< T > ROOT::TThreadedObject< T >::Get ( )
inline

Access the pointer corresponding to the current slot.

This method is not adequate for being called inside tight loops as it implies a lookup in a mapping between the threadIDs and the slot indices. A good practice consists in copying the pointer onto the stack and proceed with the loop as shown in this work item (psudo-code) which will be sent to different threads:

auto workItem = [](){
auto objPtr = tthreadedObject.Get();
for (auto i : ROOT::TSeqI(1000)) {
// tthreadedObject->FastMethod(i); // don't do this! Inefficient!
objPtr->FastMethod(i);
}
}
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66

Definition at line 229 of file TThreadedObject.hxx.

◆ GetAtSlot()

template<class T >
std::shared_ptr< T > ROOT::TThreadedObject< T >::GetAtSlot ( unsigned  i)
inline

Access a particular processing slot.

This method is thread-unsafe: it cannot be invoked from two different threads with the same argument.

Definition at line 175 of file TThreadedObject.hxx.

◆ GetAtSlotRaw()

template<class T >
T * ROOT::TThreadedObject< T >::GetAtSlotRaw ( unsigned  i) const
inline

Access a particular slot which corresponds to a single thread.

This overload is faster than the GetAtSlotUnchecked method but the caller is responsible to make sure that an object is initialised for the particular slot and that the returned pointer will not outlive the TThreadedObject that returned it.

Definition at line 209 of file TThreadedObject.hxx.

◆ GetAtSlotUnchecked()

template<class T >
std::shared_ptr< T > ROOT::TThreadedObject< T >::GetAtSlotUnchecked ( unsigned  i) const
inline

Access a particular slot which corresponds to a single thread.

This is in general faster than the GetAtSlot method but it is responsibility of the caller to make sure that an object is initialised for the particular slot.

Definition at line 199 of file TThreadedObject.hxx.

◆ GetThisSlotNumber()

template<class T >
unsigned ROOT::TThreadedObject< T >::GetThisSlotNumber ( )
inlineprivate

Get the slot number for this threadID.

Definition at line 282 of file TThreadedObject.hxx.

◆ Merge()

template<class T >
std::shared_ptr< T > ROOT::TThreadedObject< T >::Merge ( TThreadedObjectUtils::MergeFunctionType< T >  mergeFunction = TThreadedObjectUtils::MergeTObjects<T>)
inline

Merge all the thread private objects.

Can be called once: it does not create any new object but destroys the present bookkeping collapsing all objects into the one at slot 0.

Definition at line 243 of file TThreadedObject.hxx.

◆ operator->()

template<class T >
T * ROOT::TThreadedObject< T >::operator-> ( )
inline

Access the wrapped object and allow to call its methods.

Definition at line 235 of file TThreadedObject.hxx.

◆ SetAtSlot()

template<class T >
void ROOT::TThreadedObject< T >::SetAtSlot ( unsigned  i,
std::shared_ptr< T >  v 
)
inline

Set the value of a particular slot.

Definition at line 190 of file TThreadedObject.hxx.

◆ SnapshotMerge()

template<class T >
std::unique_ptr< T > ROOT::TThreadedObject< T >::SnapshotMerge ( TThreadedObjectUtils::MergeFunctionType< T >  mergeFunction = TThreadedObjectUtils::MergeTObjects<T>)
inline

Merge all the thread private objects.

Can be called many times. It does create a new instance of class T to represent the "Sum" object. This method is not thread safe: correct or acceptable behaviours depend on the nature of T and of the merging function.

Definition at line 259 of file TThreadedObject.hxx.

Member Data Documentation

◆ fCurrMaxSlotIndex

template<class T >
unsigned ROOT::TThreadedObject< T >::fCurrMaxSlotIndex = 0
private

The maximum slot index.

Definition at line 277 of file TThreadedObject.hxx.

◆ fDirectories

template<class T >
std::vector<TDirectory*> ROOT::TThreadedObject< T >::fDirectories
private

A TDirectory per thread is kept.

Definition at line 275 of file TThreadedObject.hxx.

◆ fgMaxSlots

template<class T >
unsigned ROOT::TThreadedObject< T >::fgMaxSlots = 64
static

The maximum number of processing slots (distinct threads) which the instances can manage.

Definition at line 155 of file TThreadedObject.hxx.

◆ fIsMerged

template<class T >
bool ROOT::TThreadedObject< T >::fIsMerged = false
private

Remember if the objects have been merged already.

Definition at line 278 of file TThreadedObject.hxx.

◆ fMaxSlots

template<class T >
unsigned ROOT::TThreadedObject< T >::fMaxSlots
private

The size of the instance.

Definition at line 272 of file TThreadedObject.hxx.

◆ fModel

template<class T >
std::unique_ptr<T> ROOT::TThreadedObject< T >::fModel
private

Use to store a "model" of the object.

Definition at line 273 of file TThreadedObject.hxx.

◆ fObjPointers

template<class T >
std::vector<std::shared_ptr<T> > ROOT::TThreadedObject< T >::fObjPointers
private

A pointer per thread is kept.

Definition at line 274 of file TThreadedObject.hxx.

◆ fThrIDSlotMap

template<class T >
std::map<std::thread::id, unsigned> ROOT::TThreadedObject< T >::fThrIDSlotMap
private

A mapping between the thread IDs and the slots.

Definition at line 276 of file TThreadedObject.hxx.

◆ fThrIDSlotMutex

template<class T >
ROOT::TSpinMutex ROOT::TThreadedObject< T >::fThrIDSlotMutex
private

Mutex to protect the ID-slot map access.

Definition at line 279 of file TThreadedObject.hxx.

Libraries for ROOT::TThreadedObject< T >:
[legend]

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