Stopping a thread

Discussions related to Visual Prolog
Steve Lympany
VIP Member
Posts: 119
Joined: 31 Mar 2001 23:01

Stopping a thread

Unread post by Steve Lympany » 10 Oct 2011 17:13

Hi,
I have an app which does a calculation in a thread. I am trying to allow the user to issue "stop" to terminate the calculation (which may last from a couple of minutes to an hour+ to complete). It lets the user get on with other things (in the app).

So, I could issue a Thread:terminate(), but the help file says that this in not advisable.

Any suggestions?

Addendum
Woops - sorry. Just reading this:

http://wiki.visual-prolog.com/index.php ... e/Monitors

cheers
Steve

User avatar
Thomas Linder Puls
VIP Member
Posts: 1622
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls » 11 Oct 2011 11:34

I am not sure exactly how the monitors help you.

If a thread is terminated with terminate the stack is not removed (as the help says). So solution is to make the tread finish its computation instead.

A simple way terminate from deep nesting is by raising an exception (and handling it).

This little test program does not have a memory problem:

Code: Select all

clauses     run() :-         foreach std::repeat() do             T01 = r(),             T02 = r(),             T03 = r(),             T04 = r(),             T05 = r(),             T06 = r(),             T07 = r(),             T08 = r(),             T09 = r(),             T10 = r(),             stdio::write("."),             T01:value := true,             T02:value := true,             T03:value := true,             T04:value := true,             T05:value := true,             T06:value := true,             T07:value := true,             T08:value := true,             T09:value := true,             T10:value := true,             memory::garbageCollect()         end foreach.   class predicates     r : () -> varM{boolean}. clauses     r() = V :-         V = varM::new(false),         _ = thread::start({ :- p(V) }).     class predicates     p : (varM{boolean}). clauses     p(V) :-         try             foreach std::repeat() do                 if true = V:value then                     common_exception::raise_error(classInfo, predicate_name(), "end")                 end if             end foreach         catch _ do             succeed         end try.
A similar program where the 10 threads are terminated instead eats memory.
Regards Thomas Linder Puls
PDC

Steve Lympany
VIP Member
Posts: 119
Joined: 31 Mar 2001 23:01

Unread post by Steve Lympany » 18 Oct 2011 20:04

Hi Thomas,
Thanks - I will try it out.
cheers
Steve

Post Reply