ExecuteThreadedLink

From OpenCog
Jump to: navigation, search

The ExecuteThreadedLink is a kind of executable link that, when executed, will execute its arguments in other threads, and collect up the return values in a LinkValue and return that as the result of execution. The maximum number of threads to use can be optionally specified. This is perhaps the easiest way to get parallel execution in Atomese; its slightly more general and perhaps easier to use than ParallelLink and ThreadJoinLink.

For example:

(cog-execute!
  (ExecuteThreaded
     (Set
        (Meet
           (TypedVariable (Variable "X") (Type 'Concept))
           (Inheritance (Variable "X") (Concept "mineral")))
        (Meet
           (TypedVariable (Variable "X") (Type 'Concept))
           (Inheritance (Variable "X") (Concept "plant"))))
        (Meet
           (TypedVariable (Variable "X") (Type 'Concept))
           (Inheritance (Variable "X") (Concept "animal"))))
  )))

will perform three searches, one for plants, one for minerals, and one for animals, in three different threads. The results will be returned in a LinkValue holding three result sets.

Optionally, the search can be limited to using just two threads, by saying

 (ExecuteThreaded
      (NumberNode 2)
      (Set ... same as above ...))


See examples/atomspace/threaded.scm for a fully working example using the above.

See also