Book Image

Mastering Julia

Book Image

Mastering Julia

Overview of this book

Table of Contents (17 chapters)
Mastering Julia
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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]
    produce(fib[i])
  end
  produce(-1)
end

This function computes the first 10 numbers in the Fibonacci series. When this function is used to create a task, it will halt at each produce() statement until the value being signaled is...