O'Reilly logo

Mastering Julia by Malcolm Sherrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required