Help getting up and running with multithreading

Roughly, there are three ways to wait for some thread to have done something.

  1. Use thread_join/1 to wait for the thread that does the work to complete
  2. Have the thread that does the work send some message to a message queue and wait for this message to arrive (that seems most appropriate here).
  3. Use thread_wait/2 to wait for a condition on the (dynamic) database. thread_wait/2 takes a goal that must become true to cause the wait to end and a list of options that tell it when to reevaluate this goal.

(1) is comparable to what you would do when spawning processes. (2) is comparable to using pipes to connect the process input of one process to the output of another and (3) relates to POSIX condition variables.

1 Like