# TBarrier and TMutexPool

**URL:** <https://root-forum.cern.ch/t/tbarrier-and-tmutexpool/19451>\
**Category:** Evolution Proposals, Apps & Ideas\
**Created:** [July 1, 2015, 6:59am UTC](https://root-forum.cern.ch/t/tbarrier-and-tmutexpool/19451 "2015-07-01T06:59:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkerdels](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/9fc348/32.png) [@jkerdels](https://root-forum.cern.ch/u/jkerdels)\
**Post date:** [July 1, 2015, 6:59am UTC](https://root-forum.cern.ch/t/tbarrier-and-tmutexpool/19451/1 "2015-07-01T06:59:12Z")

</div>

Hi everyone,

Some time ago I’ve written two helper classes to improve the usage of threads in ROOT: a barrier and a mutex pool. Maybe they come in handy for you too.

To explain what the classes do, I’ll just quote my own documentation here:

// The TBarrier class provides a synchronization mechanism for  
// a group of TThreads.  
//  
// A barrier is initialized with the number of threads that have  
// to run into the barrier in order to tear the barrier down and  
// continue processing. This mechanism is typically used if a  
// stepwise task should be processed by a number of threads in  
// parallel and where the threads have to synchronize in between  
// the single steps.

// The TMutexPool class provides a means to map a huge number of resources  
// that needs mutex protection onto a fixed and usually small number of  
// mutexes.  
//  
// The main idea of a mutex pool is to provide a fixed number of mutexes  
// and distribute them based on a simple indexing scheme. The mutex pool  
// overloads the [] operator such that a mutex of the pool can be accessed  
// via an index. The index can be any number. It does NOT have to be smaller  
// than the number of mutexes in the pool. Internally, each index is used  
// modulo the number of mutexes to access a certain mutex. Thus, the only  
// requirement is, that the index used to address a mutex is constant to  
// that usage, i.e., each resource should have a constant id, e.g., provided  
// by TObject::Hash();

I’ve used these classes in Root-5, but they should work also under Root-6. 😉

Cheers,  
Jochen  
[TBarrier.cpp](https://root-forum.cern.ch/uploads/default/original/2X/f/fbe2c18bd61d57bb917ab466119fb178a915524b.cpp) (3.14 KB)  
[TBarrier.h](https://root-forum.cern.ch/uploads/default/original/2X/5/5b2e897bac3acc6eeeb72a39afacea6ec40206c7.h) (786 Bytes)  
[TMutexPool.cpp](https://root-forum.cern.ch/uploads/default/original/2X/d/d927e292237e89ae46bce28c78708833027c448e.cpp) (1.75 KB)  
[TMutexPool.h](https://root-forum.cern.ch/uploads/default/original/2X/d/d460701f007c1af731f3e26cb2fa8c2b638c2650.h) (560 Bytes)
