Project

General

Profile

Support #21393

Question about using Gallery in a program with multiple threads

Added by Gray Putnam 12 months ago. Updated 10 months ago.

Status:
Feedback
Priority:
Normal
Assignee:
-
Target version:
-
Start date:
11/17/2018
Due date:
% Done:

0%

Estimated time:
Scope:
Internal
Experiment:
SBND
SSI Package:
Duration:

Description

Hello,

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(); ev.next()) {}
  return NULL;
}

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

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) {
      filenames.push_back(filename);
    }
  }
  else {
    // Files listed on command line
    for (int i=optind; i<argc; i++) {
      filenames.push_back(argv[i]);
    }
  }

  assert(!filenames.empty());

  // 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.

History

#1 Updated by Gray Putnam 12 months ago

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

galleryThread();

#2 Updated by Kyle Knoepfel 12 months 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 12 months ago

  • Status changed from New to Feedback

#4 Updated by Kyle Knoepfel 12 months ago

  • Description updated (diff)

#5 Updated by Gray Putnam 12 months 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 12 months 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 10 months ago

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



Also available in: Atom PDF