Multi-threading: A brief introduction

From wiki.visual-prolog.com

Revision as of 20:13, 8 May 2012 by Thomas Linder Puls (talk | contribs) (New page: Multi-threading is only possible in the '''Commercial Edition'''. Threads are represented by the <vp>thread</vp> class/interface. And it is very simple to create a new thread: <vip>clau...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Multi-threading is only possible in the Commercial Edition.

Threads are represented by the thread class/interface. And it is very simple to create a new thread:

clauses
        ...
        T = thread::start(myThread), % start a new thread which executes the predicate myThread
        ...
 
predicates
    myThread : ().
clauses
   myThread() :-
        ...

Using anonymous predicates it is also very simple to transfer data to the new thread:

clauses
        ...
        SomeData = ...,
        T = thread::start( { :- myThreadWithData(SomeData) }),
        ...
 
predicates
    myThreadWithData : (complexData ThreadInputData).
clauses
   myThreadWithData(ThreadInputData) :-
        ...

You can wait for thread termination, because a thread object is a synchronization object:

clauses
        ...
        SomeData = ...,
        T = thread::start( { :- myThreadWithData(SomeData) } ),
        ... % stuff done in parallel with T
        T:wait(),  % Wait for T to terminate
        ...

Thread safe acces to shared data can be done using monitors and/or synchronization objects like criticalSection, event, mutex and semaphore.

See also

Thread safety in Visual Prolog