Immortal thread?

Hello,

please help me with probably quite stupid problem. I make a thread (by time it should be an infinite loop reading data from a detector) and some time later I wanna stop it. I tried the following simple example - just counting from 0 to 9, one by one each second. Then, I try to kill the thread 5 seconds later. But it survives and finishes at 9.

I use ROOT 5.13/01 (today’s built with --enable-thread and --enable-qt options) and CERN SL4 Linux. To run the example I used standard ACLiC .x why.cpp++ . I also tried methods Delete and Exit and SetCancelOn, but no success.

[code]#include “TThread.h”

void* ThreadFunction(void *)
{
for (int i = 0; i < 10; i++) {
printf("%i\n", i);
sleep(1);
}
return NULL;
}

void why()
{
// run the thread
TThread *t = new TThread(ThreadFunction, NULL);
t->Run();

// wait 5 s and try to kill the thread
sleep(5);
t->Kill();
}[/code]

Thank you for any suggestions,
Kaspi.

Hi Kaspi,
This code works :

[code]
#include “TThread.h”

void* ThreadFunction(void *)
{
for (int i=0; i<10>; i++) {
printf("%i\n", i);
gSystem->Sleep(1000);
}
return NULL;
}

void why()
{
// run the thread
TThread *t = new TThread(ThreadFunction, NULL);
t->Run();

// wait 5 s and try to kill the thread
gSystem->Sleep(5000);
t->Kill();
}[/code]
Cheers,
Bertrand.

Hello Bertrand,

thank you very much for you suggestion, but unfortunatelly, it doesn’t work on my machine. I mean the thread is not interupted and still count up to 9. I also tried the official build of ROOT 5.12 available at AFS to make sure the problem is not on side of my own build. But still same. Do you have any idea why? Btw, what is the difference between sleep() and TSystem::Sleep() ? Thank you very much,

Kaspi

[quote=“bellenot”]Hi Kaspi,
This code works :

[code]
#include “TThread.h”

void* ThreadFunction(void *)
{
for (int i=0; i<10>; i++) {
printf("%i\n", i);
gSystem->Sleep(1000);
}
return NULL;
}

void why()
{
// run the thread
TThread *t = new TThread(ThreadFunction, NULL);
t->Run();

// wait 5 s and try to kill the thread
gSystem->Sleep(5000);
t->Kill();
}[/code]
Cheers,
Bertrand.[/quote]

Hi,

Well, first of all, I tested your code on a Windows machine, and sleep() is not portable…
Then, TSystem::Sleep() is handled by ROOT, and (at least on Windows) it allows the processor to switch to any another waiting thread…
I will try tomorrow on SLC3 and let you know.

Cheers,
Bertrand.

Hi Kaspi,

Strange, it doesn’t work on SLC3…
Anyway, please take a look at the working solution in attachment.

Cheers,
Bertrand.
why.C (514 Bytes)

Hello,

thank you very much for helping me. Unfortunately, I still have problems to incorporate your working solution into my project. The thread should be run and terminated by user via Qt GUI. That’s why I need to compile it. I tried to add

int main()
{
  why();
  return 0;
}

to the end of your solution. Then, I compiled and ran it by

gcc `root-config --libs` `root-config --cflags` -lThread why.C
./a.out

The beahvior was wierd, each time different, the counting never started, and it often finished with “segmentation fault”. Do you have a idea what’s wrong?

Thaks again, Kaspi.

Please use this format :

int main(int argc, char **argv) { TApplication theApp("App", &argc, argv); why(); theApp.Run(); return 0; }
And add “gApplication->Terminate();” at the end of the function “void *top(void *)” if you want to terminate application when threads are over.

Cheers,
Bertrand.

Great ! Thank you very much for your help. I managed to implement a ROOT thread into my Qt project :slight_smile: For those who have similar problem, I wanna summarize the key points.

  1. It was crucial to add TThread::CancelPoint() to the thread function.
  2. For safe thread deletion I had to isert a small break

MyThread->Kill(); TThread::Sleep(1, 0); delete MyThread;

Good luck, Kašpi.