Re: [ROOT] Starting TThreads

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Jun 11 2003 - 18:18:57 MEST


Hi Volker,

Interesting finding!
I believe that you get this problem when running without a TApplication 
object. See your code below modified to include a TApplication
and it runs without problems with or without gDebug=1;

Rene Brun


#include "TApplication.h"
#include "TThread.h"
#include <iostream>

void* handle(void* ptr) {
  int nr = (int) ptr;

  while (1) {
    TThread::Lock();
    std::cerr << "Here I am: " << nr << std::endl;
    TThread::UnLock();
    sleep(1);
  }
}

int main() {
   TApplication app("app",0,0);
   gDebug = 1;

  std::cerr << "Starting Thread 1" << std::endl;
  TThread *h1 = new TThread("h1", handle, (void*) 1);
  h1->Run();
  std::cerr << "Starting Thread 2" << std::endl;
  TThread *h2 = new TThread("h2", handle, (void*) 2);
  h2->Run();
  std::cerr << "Starting Thread 3" << std::endl;
  TThread *h3 = new TThread("h3", handle, (void*) 3);
  h3->Run();
  app.Run();
  //while(1) {}
}


On Wed, 11 
Jun 2003, Volker Hejny wrote:

> Dear all,
> 
> in some recent ROOT version I observed, that some debug output using
> TThread::Printf(...) in TThread::Fun() is now surrounded by an
> if (gDebug) { ... } statement. This reminds me of some old question
> of mine, which I want to repeat now.
> 
> Our original problem was, that after a TThread::Run() the correspoding
> thread kept hanging in the first TThread::Printf() call in the
> function TThread::Fun(). Only when commenting out these Printf() calls
> we got our programs run - without any further problems with TThread
> during the last 1 1/2 years. However, we always had to comment out
> these lines in TThread::Fun().
> 
> Consider the following small program:
> 
> #include "TThread.h"
> #include <iostream>
> 
> void* handle(void* ptr) {
>   int nr = (int) ptr;
> 
>   while (1) {
>     TThread::Lock();
>     std::cerr << "Here I am: " << nr << std::endl;
>     TThread::UnLock();
>     sleep(1);
>   }
> }
> 
> int main() {
>   // gDebug = 1;
> 
>   std::cerr << "Starting Thread 1" << std::endl;
>   TThread *h1 = new TThread("h1", handle, (void*) 1);
>   h1->Run();
>   std::cerr << "Starting Thread 2" << std::endl;
>   TThread *h2 = new TThread("h2", handle, (void*) 2);
>   h2->Run();
>   std::cerr << "Starting Thread 3" << std::endl;
>   TThread *h3 = new TThread("h3", handle, (void*) 3);
>   h3->Run();
>   while(1) {}
> }
> 
> In this configuration (with the actual cvs version of ROOT) everything
> runs fine on all machines (various Linux distributions). But if one 
> enables 'gDebug = 1' the main program reaches the while-loop, but
> the threads are not running but sticking in the TThread::Fun() function.
> 
> Since nobody else was complaining about such a behaviour, I assume
> that the this does not occur for other people. Any clue?
> 
> Best regards,
> Volker
> 
> 



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:12 MET