Tasks

Tasks (aka co-routines) form the basis for Julia's provision of parallel processing. They are sometimes referred to as lightweight or green threads. When some code is executed as a task, it is possible to suspend it and switch to another task. The original task can be resumed and will continue from where it was suspended.

Tasks cooperate by using a producer-consumer mechanism. A producer task will halt at a point where it has some values, which need to be consumed, and a separate task will be able to access these values. Producer and consumer tasks can both continue to run by exchanging values as necessary.

function fibs(n = 10)
  fib = int64(zeros(n))
  fib[1] = 1
  produce fib[1)
  fib[2]
  produce fib[2]
  for i = 3:n
 fib[i] = fib[i-1] + fib[i-2] ...

Get Mastering Julia now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.