A wrapper to make object instances thread private, lazily.
T | Class 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 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.
Definition at line 151 of file TThreadedObject.hxx.
Public Member Functions | |
template<class ... ARGS> | |
TThreadedObject (ARGS &&... args) | |
Construct the TThreadedObject and the "model" of the thread private objects. | |
TThreadedObject (const TThreadedObject &)=delete | |
template<class... ARGS> | |
TThreadedObject (TNumSlots initSlots, ARGS &&... args) | |
Construct the TThreadedObject with initSlots empty slots and the "model" of the thread private objects. | |
std::shared_ptr< T > | Get () |
Access the pointer corresponding to the current slot. | |
std::shared_ptr< T > | GetAtSlot (unsigned i) |
Access a particular processing slot. | |
T * | GetAtSlotRaw (unsigned i) const |
Access a particular slot which corresponds to a single thread. | |
std::shared_ptr< T > | GetAtSlotUnchecked (unsigned i) const |
Access a particular slot which corresponds to a single thread. | |
unsigned | GetNSlots () const |
Return the number of currently available slot. | |
std::shared_ptr< T > | Merge (TThreadedObjectUtils::MergeFunctionType< T > mergeFunction=TThreadedObjectUtils::MergeTObjects< T >) |
Merge all the thread private objects. | |
T * | operator-> () |
Access the wrapped object and allow to call its methods. | |
void | SetAtSlot (unsigned i, std::shared_ptr< T > v) |
Set the value of a particular slot. | |
std::unique_ptr< T > | SnapshotMerge (TThreadedObjectUtils::MergeFunctionType< T > mergeFunction=TThreadedObjectUtils::MergeTObjects< T >) |
Merge all the thread private objects. | |
Static Public Attributes | |
static constexpr const TNumSlots | fgMaxSlots {64} |
The initial number of empty processing slots that a TThreadedObject is constructed with by default. | |
Private Member Functions | |
unsigned | GetThisSlotNumber () |
Get the slot number for this threadID, make a slot if needed. | |
Private Attributes | |
std::deque< TDirectory * > | fDirectories |
A TDirectory per slot. | |
bool | fIsMerged: 1 |
Remember if the objects have been merged already. | |
std::unique_ptr< T > | fModel |
Use to store a "model" of the object. | |
std::deque< std::shared_ptr< T > > | fObjPointers |
An object pointer per slot. | |
ROOT::TSpinMutex | fSpinMutex |
Protects concurrent access to fThrIDSlotMap, fObjPointers. | |
std::map< std::thread::id, unsigned > | fThrIDSlotMap |
A mapping between the thread IDs and the slots. | |
#include <ROOT/TThreadedObject.hxx>
|
delete |
|
inline |
Construct the TThreadedObject with initSlots empty slots and the "model" of the thread private objects.
initSlots | Set the initial number of slots of the TThreadedObject. |
ARGS | Arguments of the constructor of T |
This form of the constructor is useful to manually pre-set the content of a given number of slots when used in combination with TThreadedObject::SetAtSlot().
Definition at line 166 of file TThreadedObject.hxx.
|
inline |
Construct the TThreadedObject and the "model" of the thread private objects.
ARGS | Arguments of the constructor of T |
Definition at line 183 of file TThreadedObject.hxx.
|
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:
Definition at line 279 of file TThreadedObject.hxx.
|
inline |
Access a particular processing slot.
This method is thread-safe as long as concurrent calls request different slots (i.e. pass a different argument) and no thread accesses slot i
via the arrow operator, so mixing usage of GetAtSlot with usage of the arrow operator can be dangerous.
Definition at line 201 of file TThreadedObject.hxx.
|
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 the slot exists, to check that the contained object is initialized and that the returned pointer will not outlive the TThreadedObject that returned it, which maintains ownership of the actual object.
Definition at line 259 of file TThreadedObject.hxx.
|
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 the slot exists and to check that the contained object is initialized (and not a nullptr).
Definition at line 248 of file TThreadedObject.hxx.
|
inline |
Return the number of currently available slot.
The method is safe to call concurrently to other TThreadedObject methods. Note that slots could be available but contain no data (i.e. a nullptr) if they have not been used yet.
Definition at line 190 of file TThreadedObject.hxx.
|
inlineprivate |
Get the slot number for this threadID, make a slot if needed.
Definition at line 337 of file TThreadedObject.hxx.
|
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 293 of file TThreadedObject.hxx.
|
inline |
Access the wrapped object and allow to call its methods.
Definition at line 285 of file TThreadedObject.hxx.
|
inline |
Set the value of a particular slot.
This method is thread-safe as long as concurrent calls access different slots (i.e. pass a different argument) and no thread accesses slot i
via the arrow operator, so mixing usage of SetAtSlot with usage of the arrow operator can be dangerous.
Definition at line 226 of file TThreadedObject.hxx.
|
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 311 of file TThreadedObject.hxx.
|
private |
A TDirectory per slot.
Definition at line 331 of file TThreadedObject.hxx.
|
staticconstexpr |
The initial number of empty processing slots that a TThreadedObject is constructed with by default.
Deprecated: TThreadedObject grows as more slots are required.
Definition at line 155 of file TThreadedObject.hxx.
|
private |
Remember if the objects have been merged already.
Definition at line 334 of file TThreadedObject.hxx.
|
private |
Use to store a "model" of the object.
Definition at line 326 of file TThreadedObject.hxx.
|
private |
An object pointer per slot.
Definition at line 328 of file TThreadedObject.hxx.
|
mutableprivate |
Protects concurrent access to fThrIDSlotMap, fObjPointers.
Definition at line 333 of file TThreadedObject.hxx.
|
private |
A mapping between the thread IDs and the slots.
Definition at line 332 of file TThreadedObject.hxx.