TClonesArray of TParticle in multi-threaded application

Dear all,

I am writing some code for a multi-thread application, and I am having problems (i.e. crashes) each time I manipulate TClonesArrays.

The objects in the arrays are TParticle. I do not use TThread, but pthreads directly.

I managed to reproduce the problem in a simple program where I start a number of threads, each one creating on the heap its own TClonesArray, then filling it with TParticles, then Clear-ing it.

Is there any known limitation to the usage of TClonesArrays (or TParticles) with more than one thread? I attach my example code.

Thanks for your help,

Andrea.

PS: root 5.34/03, configured with --with-thread-libdir=/usr/lib64/ --all

PPS: I did see this thread

but it is not clear (to me) if that recipe is only needed for writing to file
testcode.cc (1.3 KB)

[quote=“disimone”]Dear all,

I am writing some code for a multi-thread application, and I am having problems (i.e. crashes) each time I manipulate TClonesArrays.

[/quote]

I think, TClonesArray is not guaranteed to be thread-safe - it obviously works with some shared (global) data, since it needs information about the contained object’s type (this can be fixed, though),
so even if you have per-thread TClonesArray, your code is still not thread-safe. On my machine/with my compilers/OS/etc. your code is not even able to create any array.

The simplest possible workaround for your small code-snippet is just to create/destroy all per-thread arrays BEFORE/AFTER parallel part, in a main thread. For me it works, though output in stdout is mixed - but this is something else :slight_smile:) (see code attached).
testcode.cc (1.2 KB)