Parallelizing using Threads

Hi everyone.
I would like to parallelize some Freeglut options with a ROOT Canvas: with Freeglut I want to display a window which shows some spheres moving (a gas), and with ROOT I want do display some graphs with the varying parameters of the simulation, such as density, temperature etc. Up to now I’ve managed to create the two windows and displaying everything (spheres and graphs in a canvas). The problems arise when I try to close the windows or, for example, to move with the mouse the graphs inside the canvas: if I close either the Glut window with the “x” or the ROOT window via “File–>Quit” command the programs goes in a “segmentation fault” like error; even if I move the graphs with the mouse I get the same error but surprisingly the program doesn’t crash and continues working. Sometimes, if I close the ROOT window via “Quit” command I don’t get any error but the program doesn’t stop: it seems that It remains stuck in the Glt threads, and in order to close it I have to Ctrl+C in the Terminal.

Now comes the question: is there a safe way to close both windows so that closing one thread automatically stops the other and doesn’t cause any error? For example: can I know when I quit ROOT via “Quit”, so that I can communicate it (i.e. changing a variable) to the “Glt thread” and make it stop?

I add a sketch of the code and upload a txt with the error.

void init() ...  //Glut functions which handle the glut window.
void idle() ...
void display() ...
void * GLT_thread(void *)
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("red 3D lighted cube");

void * graph_thread(void *)
   //Here I initialize some variables, read data from a file and fill them in graphs, then draw the graph;
  //this process is "dynamic", in the sense that graphs update themselves with new data read from file until the file is over (for example: a graph shows the density of the gas varying with time)
int main(int argc, char **argv)
  //Initialization of the dimensions of the windows and other display parameters
  glutInit(&argc, argv);
  TApplication root_app("Statistics", &argc, argv);
  canvas = new TCanvas("Graph", "Graph", screen_width/2, window_posy, window_width, window_height);
  TThread thread1("Graph display", graph_thread);
  TThread thread2("GLT", GLT_thread);
  return 0;

Thanks in advance.
Error.txt (5.3 KB)


In order to use ROOT with threads you need to call ROOT::EnableThreadSafety(); in your code.
I recommend using std::thread (#include <thread> and use C++11) instead of TThread.
You may also want to use a condition variable to check for the end of the execution loop instead
of killing the threads (see std::condition_variable for more information).

Thanks a lot for the answer.
I’ve considered using std’s threads, but I remember having problems with the fact that ROOT is managed via TApplication, (if I’m not wrong, when the program “entered” TApplication in a thread it stopped, but I’m not sure). I will try with std and maybe find a solution.
Thank you again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.