Re: Aborting without killing
Jan Wielemaker <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 11/14/2013 01:44 PM, Torbjörn Lager wrote:
> Hi,
>
> Running (say)
>
> ?- repeat, fail.
>
> and executing thread_signal(main, abort) from another thread results in
>
> % Execution Aborted
>
> However, main is still running.
>
> However, running
>
> ?- thread_create((repeat, fail), ID, [alias(mymain)]).
>
> and executing thread_signal(mymain, abort) from another thread results
> in mymain terminating with an exception '$aborted'.
>
> So, is there a way to abort a currently running goal without also
> killing the thread that runs it? Or is the thread 'main' in the above
> example killed and then recreated again? Is that what is happening?
The main thread runs the C-function PL_toplevel(), which restarts the
engine if it died on an exception. This is the same for other exceptions:
an exception does not kill the interactive toplevel, but does kill any
other thread when not caught.
What is special about abort, which is implemented as throw('$aborted'),
is that catch wraps the recover goal to re-throw the exception after
the recover goal has terminated. So, you can cheat and make sure the
recover goal never terminates :-)
The big question of course is why you'd like to use abort if you don't
want to kill the thread. If you just want it stop the thread doing some
task and start another, simply do signal(Target, throw(stop)) and in the
target, you do
catch(Goal, stop, <whatever>)
Cheers --- Jan