TThread question

Hi,

I want to be able to stop Kill() a thread and then later restart it.

I would assume that once TThread::GetState returns TThread::kCanceledState that I can proceed, however TThread::GetState just returns TThread::kCancelingState - which seems to imply that it is still doing something.

Interestingly, if I wait for TThread::kCancelingState I seem to be able to Run() my thread again - but I just want to be sure what I am doing is ok since it seems wrong. Simple example attached.

Thanks
Peter
testKillRestartThread.C (576 Bytes)

looks to me like a bug. The thread is actually canceled, but the state flag has not been changed.
I am going to check the source code… Probably something is wrong with the cleanup function. I will report back to you when the bug is fixed.

This is Ubuntu 10.04.4 LTS i686 (gcc 4.4.3) here … ROOT 5.28 / 5.30 / 5.33 …

root [0] .L testKillRestartThread.C++ Info in <TUnixSystem::ACLiC>: creating shared library /..././testKillRestartThread_C.so root [1] test() Starting func in do loop in do loop in do loop in do loop in do loop Waiting to be canceled Starting func in do loop in do loop in do loop in do loop in do loop in do loop in do loop in do loop in do loop in do loop root [2] .q
testKillRestartThread.C (678 Bytes)

Peter, do you run this code on OSX or Linux?

[quote=“Pepe Le Pew”]This is Ubuntu 10.04.4 LTS i686 (gcc 4.4.3) here … ROOT 5.28 / 5.30 / 5.33 …

root [0] .L testKillRestartThread.C++ Info in <TUnixSystem::ACLiC>: creating shared library /..././testKillRestartThread_C.so root [1] test() Starting func in do loop in do loop in do loop in do loop in do loop Waiting to be canceled Starting func in do loop in do loop in do loop in do loop in do loop in do loop in do loop in do loop in do loop in do loop root [2] .q[/quote]
What happens if you put TThread::CancelPoint(); somewhere in the loop of the funcToKill ?

No change in behavior here.

Hi,

I am running OS X 10.7.2, root 5.30/02 and gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

Just to confirm - Pepe’s version hangs for me.

thanks
Peter

[quote=“petercogan”]Hi,

I am running OS X 10.7.2, root 5.30/02 and gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

Just to confirm - Pepe’s version hangs for me.

thanks
Peter[/quote]
That is exactly what I thought. I am now also testing it on OSX and can reproduce this behavior.

Can you try:
getconf GNU_LIBC_VERSION
getconf GNU_LIBPTHREAD_VERSION
I get “glibc 2.11.1” and “NPTL 2.11.1” here (an “up-to-date” Ubuntu 10.04.4 LTS i686 SMP).

Sorry those commands don’t seem to work -

hades:~ peter$ getconf GNU_LIBC_VERSION
getconf: no such configuration parameter GNU_LIBC_VERSION' hades:~ peter$ getconf GNU_LIBPTHREAD_VERSION getconf: no such configuration parameterGNU_LIBPTHREAD_VERSION’

Am trying to figure out how to check these on os x - if someone already knows how please let me know

Hi,

not having much luck figuring out how to check the version of libc - although I did read that NPTL doesn’t exist of OS X, so I think we can assume that the versions will be the same as each other - just can’t figure out what it is…

[quote=“petercogan”]Hi,

not having much luck figuring out how to check the version of libc - although I did read that NPTL doesn’t exist of OS X, so I think we can assume that the versions will be the same as each other - just can’t figure out what it is…[/quote]

I would advise to use conditional variables (root.cern.ch/root/html532/TCondition.html) to stop threads and restart when needed.
Using cancellation of threads in anyway is not a cleanest way at all.

In meanwhile I will try trace down the reason for cancel not to work on OSX. BTW, I am a bit puzzled, because the current implementation shouldn’t work on Linux as well, but it does :slight_smile:

Maybe one should verify that these work:
${ROOTSYS}/test/threads.cxx
${ROOTSYS}/tutorials/thread/*

BTW. Try … getconf -a

Sorry , -a doesn’t help

hades:~ peter$ getconf -a GNU_LIBC_VERSION
getconf: illegal option – a
usage: getconf [-v prog_env] system_var
getconf [-v prog_env] path_var pathname
hades:~ peter$

The tutorials and test program seem to run fine.

Regarding killing a thread - the reason I do it is because I have a function that may time out. So I have two threads ; the first runs my function and the second is just a timer. If some time limit is exceeded, the timer thread kills the first thread and then just restarts it.

Maybe it’s possible to achieve this with conditions but I can’t figure out how.

thanks
Peter

[quote=“petercogan”]The tutorials and test program seem to run fine.

Regarding killing a thread - the reason I do it is because I have a function that may time out. So I have two threads ; the first runs my function and the second is just a timer. If some time limit is exceeded, the timer thread kills the first thread and then just restarts it.

Maybe it’s possible to achieve this with conditions but I can’t figure out how.

thanks
Peter[/quote]
Sounds good! Like I said, exactly for your case, you either should use conditional variables (an elegant solution) or just define a sig_atomic_t variable to check in the thread whether it needs to exit.

But in all cases, try to avoid using cancelation (killing), always gracefully exit threads.