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.
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)
[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 ?
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).
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
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…
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
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.
[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.