Root I/O and thread safety

Hi,

Is Root I/O in principle thread-safe ?
For instance, am I supposed to be able to read 2 files in 2 different threads running at the same time ? (1 file per thread).
I ask because I tried with ALICE raw data files (Root format), and get various kind of crashes…

Regards,

Hi,

[quote]Is Root I/O in principle thread-safe ? [/quote]The academic answer is no, however following some simple rules, you should be able to read 2 files in 2 different threads at the same time.

First note that gFile and gDirectory are global variable and are not (yet) thread protected. So in your case do not use any of the interface that rely on the value of gFile/gDirectory. One example of such function is TObject::Write.

Second you need to make sure that all library and all StreamerInfo are loaded serially. This can be done in large part, by opening the TFile before starting the threads.

In addition, ROOT I/O does run some user code (classes default constructor, custom streamer if any). This user code (i.e. Alice code I assume in your case).

Cheers,
Philippe.

Hi Philippe,

Thanks for your answer.

If I really must use gFile and/or gDirectory, can I simply use R__LOCKGUARD to protect those pieces of code ?

That should be doable, yes. I assume here that I can open and close the file (before entering the thread that would reopen it) and the effect will be the same, is that true ?

Seems your message was truncated here. I assume you meant that user code itself must be thread safe as well :wink:

Regards,

[quote]If I really must use gFile and/or gDirectory, can I simply use R__LOCKGUARD to protect those pieces of code ? [/quote]Yes it should if this is done properly.

[quote]
That should be doable, yes. I assume here that I can open and close the file (before entering the thread that would reopen it) and the effect will be the same, is that true ? [/quote]Yes.

[quote]Seems your message was truncated here.[/quote]Yes it was!

I meant to say :
In addition, ROOT I/O does run some user code (classes default constructor, custom streamer if any). This user code (i.e. Alice code I assume in your case) must also be thread safe

Cheers,
Philippe