ParallelLink

From OpenCog
Jump to: navigation, search

The ParallelLink is a kind of Link that, when evaluated, will evaluate each atom in its outgoing set in its own unique thread. It does not wait for the evaluations to complete; instead it implements "fire and forget" semantics. It always evaluates to a truth value of "true", immediately. The ThreadJoinLink is similar, except that it waits for all the threads to complete, and returns the logical-and of the resulting evaluations.

For example,

  (Parallel
     (SequentialAnd
        (True (Sleep (Number 1)))
        (Evaluation
           (GroundedPredicate "scm:incr") (List)))
     (SequentialAnd
        (True (Sleep (Number 3)))
        (Evaluation
           (GroundedPredicate "scm:incr") (List)))
     (SequentialAnd
        (True (Sleep (Number 5)))
        (Evaluation
           (GroundedPredicate "scm:incr") (List))))

will launch three threads, each sleeping for 1, 3 or 5 seconds, and then calling some scheme function incr. See examples/atomspace/parallel.scm for a fully working example using the above.

For many users, it might be easier to use ExecuteThreadedLink, which executes (rather than evaluates) what it wraps, and can control the number of threads to use during execution.

See also