ThreadJoinLink

From OpenCog
Jump to: navigation, search

The ThreadJoinLink is a kind of AndLink that, when evaluated, will evaluate each atom in it's outgoing set in it's own unique thread. It will then block the current thread, and wait until each of the threads completes. The resulting truth-value of the evaluation will be the boolean-and of the resulting truth-value from each thread. Thus, it literally behaves very much like an AndLink, except that the execution has been moved to other threads.

The ParallelLink is similar, except that it detaches from its threads immediately, and does not wait for their completion; it always evaluates to true.

For example,

  (ThreadJoin
     (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