Re: How to rollback another stack in C++?
chengpu@gmail.com wrote:
Hi!
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);
Does the thread have control over the code that is executed as result
of the message, or is this opaque. If it does not have control, the
possibility exists that the executed code is deliberately in an
infinite loop (like for example blocking on a select statement, or
Win32 I/O Completion Port). This type of thing would certainly appear
the same, from a higher level perspective, than an infinite loop.
If I was you I would not try and control (or prohibit) the possibility
of infinite loops, but trust the higher level to not deliberately cause
this. OTOH, I suppose a thread could have an attribute that indicates
that infinite loops are not allowed. In that case I would use the
concept of a Supervisoral thread to whom all threads report
periodically. On a thread not reporting, the Supervisoral Thread
terminates and respawns it, causing the stack to rewind automatically.
(2) When the sender of the message is dead, such as when a socket
connection is lost, there is no point to continue working on the
request from that connection;
As far as this is concerned, I think sometimes a request needs to be
executed despite the requester being dead, so to speak. It is more
important that the handler of the request still be available. I think
whether or not the request should be performed, could be an attribute
of the request itself. Something like:
struct RequestValidator
{
virtual bool isApplicable() const = 0;
};
struct Request
{
bool applicable() const
{
return( myValidator_ && myValidator()->isApplicable() );
}
//Called from receiving context
virtual void execute() = 0;
//...
};
//Receiving context/thread
void X::process()
{
Request* request( getNext() );
if( request.applicable() )
{
request.execute();
}
}
Note, I omitted allot of code, just to give you an idea. Hope it helps.
Regards,
Werner
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]