Support #21393

Question about using Gallery in a program with multiple threads

Added by Gray Putnam about 2 years ago. Updated 7 months ago.

Target version:
Start date:
Due date:
% Done:


Estimated time:
SSI Package:



I am unsure if the issue tracker here is monitored. If not, I am happy to post this issue elsewhere.

I have been using gallery and have found it to be very helpful. I have a question with how gallery interacts with multiple threads.

To make the question clear I have included a minimal example reproducing the issues I have encountered. The code is an executable which reads a list of input files from the command line and opens them all in gallery.

#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>

#include <thread>
#include <TROOT.h>
#include <TThread.h>
#include "gallery/Event.h" 

std::vector<std::string> filenames;

void *galleryTThread(void *inp) {
  (void) inp;
  for (gallery::Event ev(filenames); !ev.atEnd(); {}
  return NULL;

void galleryThread() {
  for (gallery::Event ev(filenames); !ev.atEnd(); {}

int main(int argc, char* argv[]) {
  // Process input file definition
  std::string filedef = argv[1];
  std::string list_suffix = ".list";

  if (std::equal(list_suffix.rbegin(), list_suffix.rend(), filedef.rbegin())) {
    // File list
    std::ifstream infile(filedef);
    std::string filename;
    while (infile >> filename) {
  else {
    // Files listed on command line
    for (int i=optind; i<argc; i++) {


  // A: works
  // ROOT::EnableThreadSafety();
  // galleryThread();

  // B: works 
  // std::thread t(galleryThread);
  // t.join();
  // C: crashes in seg fault
  // ROOT::EnableThreadSafety();
  // std::thread t(galleryThread);
  // t.join();

  // D: crashes in seg fault
  // TThread t0(galleryTThread);
  // t0.Run();
  // t0.Join();

  return 0;

As expected, (A) works. And we can even open gallery in a separate thread using (B). However, why does enabling thread safety in ROOT prevent this from working (in (C))? And what is wrong with using a TThread in (D)?

In (C) and (D), I have found that crashes seem to occur in a destructor call to the ProductIDStreamer class. Specifically, there seems to always be a vector in the ProductIDStreamer class which has an unreasonable size (I believe it must've been corrupted somehow).

Any help that could be provided on this would be greatly appreciated.


#1 Updated by Gray Putnam about 2 years ago

Note that there is a typo in (A). The second line should just read:


#2 Updated by Kyle Knoepfel about 2 years ago

Gray, gallery is not thread-safe, and any attempt to use it with multiple threads is likely to fail. That said, what version of gallery are you using?

#3 Updated by Kyle Knoepfel about 2 years ago

  • Status changed from New to Feedback

#4 Updated by Kyle Knoepfel about 2 years ago

  • Description updated (diff)

#5 Updated by Gray Putnam about 2 years ago

Hi Kyle, thanks for getting back to me.

It makes sense that gallery is not thread safe. My confusion here came from the fact that the use of gallery in these programs is single threaded -- it just wasn't executed from the "main" thread.

I am using gallery v1.08.03 with the compiler qualifier e15:prof.

#6 Updated by Kyle Knoepfel about 2 years ago

Please look at the release notes for gallery 1.10. It does not guarantee thread-safe behavior from gallery, but it does include changes in underlying packages that were required for multi-threaded art.

#7 Updated by Kyle Knoepfel almost 2 years ago

Hi Gray, is there anything else to be addressed with this issue, or can we close it? Thanks.

#8 Updated by Kyle Knoepfel 7 months ago

  • Status changed from Feedback to Closed

Closed due to no more feedback.

Also available in: Atom PDF