Discussion:
throwing an exception... on behalf of another thread
(too old to reply)
Mycroft Holmes
2010-03-08 13:09:53 UTC
Permalink
Hi all,

this is a question specific to windows.
we'd like to induce a running thread to throw an exception; the
following fragment should give the idea:


struct bomb_explosion {};

void explode()
{
throw bomb_explosion();
}


void compute_result()
{
TIME_BOMB t(60, &explode); // after 60 seconds, call "explode"

try
{
t.activate();
perform_a_long_operation();
}
catch(bomb_explosion&)
{
std::cout << "timeout!";
}

t.deactivate();
}


we were looking at the documentation of Timer Queues, but if we got it
right, when the timer invokes the callback, it runs as a different
thread, so a throw wouldn't work.
the naive approach would be: set a (global) variable inside the
callback, and periodically check it inside
"perform_a_long_operation"; however we cannot use this pattern,
because this last function is read-only for us.

is there any windows api/trick we can use?

TIA
-mh
Igor Tandetnik
2010-03-08 13:25:08 UTC
Permalink
Post by Mycroft Holmes
this is a question specific to windows.
In what way is it specific to Windows? I don't seem to see any OS-specific elements in it.
Post by Mycroft Holmes
the naive approach would be: set a (global) variable inside the
callback, and periodically check it inside
"perform_a_long_operation"; however we cannot use this pattern,
because this last function is read-only for us.
Just as you can't inject a "check for boolean flag" statement into the function, you can't inject a throw statement into it.
Post by Mycroft Holmes
is there any windows api/trick we can use?
Not to my knowledge. Well, short of TerminateThread.

See also:

http://blogs.msdn.com/ericlippert/archive/2010/02/22/should-i-specify-a-timeout.aspx
http://blogs.msdn.com/ericlippert/archive/2010/02/25/careful-with-that-axe-part-two-what-about-exceptions.aspx
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Mycroft Holmes
2010-03-08 14:17:04 UTC
Permalink
Post by Igor Tandetnik
In what way is it specific to Windows? I don't seem to see any OS-specific elements in it.
not in the problem itself, but in the solution...
we need a solution specific for windows.
Post by Igor Tandetnik
Post by Mycroft Holmes
the naive approach would be: set a (global) variable inside the
callback, and periodically check it inside
"perform_a_long_operation"; however we cannot use this pattern,
because this last function is read-only for us.
Just as you can't inject a "check for boolean flag" statement into the function, you can't inject a throw statement into it.
maybe we can alter the thread program counter from outside...
Post by Igor Tandetnik
Not to my knowledge. Well, short of TerminateThread.
yes, sort of, but with a cleaner exit


thanks for the links.
Ben Voigt [C++ MVP]
2010-03-11 01:11:06 UTC
Permalink
Post by Mycroft Holmes
Post by Igor Tandetnik
Post by Mycroft Holmes
the naive approach would be: set a (global) variable inside the
callback, and periodically check it inside
"perform_a_long_operation"; however we cannot use this pattern,
because this last function is read-only for us.
Just as you can't inject a "check for boolean flag" statement into the
function, you can't inject a throw statement into it.
maybe we can alter the thread program counter from outside...
While the thread is suspended, I hope. In an era of multicore CPUs, just
because your thread is active doesn't mean the other thread isn't also
executing.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4933 (20100310) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Alex Blekhman
2010-03-08 14:04:57 UTC
Permalink
Post by Mycroft Holmes
we'd like to induce a running thread to throw an exception; the
There is the ancient Jeffrey Richter's article on the topic (from
March 1996 issue of MS Systems Journal):
http://www.microsoft.com/MSJ/archive/SFFF.aspx
The article demonstrates how to induce an exception on other
thread, so it can die gracefully. I haven't checked it under
Windows 7 or Vista, but AFAIK it worked under XP.

HTH
Alex
Alex Blekhman
2010-03-08 14:19:51 UTC
Permalink
Post by Mycroft Holmes
we'd like to induce a running thread to throw an exception;
Well, MS server censored my previous post for whatever reason.
Here's another try.

March 1996 issue of MS Systems Journal:
http://www.microsoft.com/MSJ/archive/SFFF.aspx

Here you'll find the article that describes what you need.

Alex
Pavel A.
2010-03-08 22:20:26 UTC
Permalink
Post by Alex Blekhman
Well, MS server censored my previous post for whatever reason.
no, it's visible on nntp.
-pa
Kenneth Porter
2010-03-11 23:23:47 UTC
Permalink
Post by Mycroft Holmes
the naive approach would be: set a (global) variable inside the
callback, and periodically check it inside
"perform_a_long_operation"; however we cannot use this pattern,
because this last function is read-only for us.
Is the function known to be exception-safe? This has all the same risks as
killing a thread instead of having it gracefully exit. Resources may be
left allocated or in a "dirty" state.

Loading...