Re: How to rollback another stack in C++?
chengpu@gmail.com wrote:
Alan Johnson wrote:
chengpu@gmail.com wrote:
I am writing a thread-safe interface class <IThread> that exchange
messages in a multi-thread environment. Assume a thread is processing
a message in a function <IThread>::ProcMsg(), in two situations I need
to roll back this function's execution on another management thread:
(1) When it is in a infinitive loop. This can be detected by how much
time the function has been executing against a predefined timeout (say
10sec);
An infinitive loop .. is that something like:
while (true) cout << "to loop " ;
No, since I am writing a base class, I have to anticipate all the
problems from the user. The infinitive loop has nothing to do with
iostream.
The prevaling solution is to kill the thread, which has two problems:
1) it leaves the object in a limbo state;
2) it does not reclaim the stack.
I'm not sure I understand the problem: is what you are trying to
do impose a time-out on the user function? That is, you start
the function in a separate thread, and if it has not finished
after a certain time, you terminate it forceably?
If so, I'm not sure there is any way to do this. There
certainly isn't in standard C++, since standard C++ doesn't
support threading. I'm most familiar with Unix, in which
there's no real solution which would involve unwinding the
stack. pthread_cancel may or may not unwind the stack,
depending on the implementation, but the user thread may be
looping in code which doesn't contain a cancellation point, and
so the cancel would be ignored. You can (under Unix) send the
other thread an asynchronous signal, but you can't raise an
exception or clean up the stack from an asynchronous signal (and
of course, a user function could explicitly mask all signals
before starting the infinite loop as well). I don't know what
Windows offers in this respect (except that I've heard that it
doesn't really offer anything equivalent to cancel), but I
rather doubt that you'll get more help. In fact, there's almost
a contradiction here: you want to unconditionally terminate the
other thread, but you want to execute its destructors. So what
happens if the other thread has an endless loop in one of its
destructors?
I'm not sure what your application is: perhaps the best solution
is to use a separate process (instead of a thread), possibly
with shared memory. Not that this solves all of your problems
either---systems like Unix may clean up all of the main memory
after process termination, but they still allow secondary memory
(e.g. temporary files) to leak.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]