From: Mycroft Holmes on
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
From: Igor Tandetnik on
Mycroft Holmes wrote:
> 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.

> 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.

> 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
From: Alex Blekhman on
"Mycroft Holmes" wrote:
> 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

From: Mycroft Holmes on
On Mar 8, 2:25 pm, "Igor Tandetnik" <itandet...(a)mvps.org> wrote:

> 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.


> > 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...



> Not to my knowledge. Well, short of TerminateThread.

yes, sort of, but with a cleaner exit


thanks for the links.

From: Alex Blekhman on
"Mycroft Holmes" wrote:
> 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

 |  Next  |  Last
Pages: 1 2
Prev: problem with bind
Next: Binary Diff Utility