Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Last active March 23, 2017 13:07
Show Gist options
  • Save yorickpeterse/2a5748b8ab5699531f23740f3881467c to your computer and use it in GitHub Desktop.
Save yorickpeterse/2a5748b8ab5699531f23740f3881467c to your computer and use it in GitHub Desktop.
(import bootstrap)
(import std integer)
(import std array)
(import std stdout)
(import std process)
; Defines the "main" module, #f indicating it has no explicit receiver. This means it will be defined
; in the default location.
(let main (defmod bootstrap "main" #f))
(let max 1000)
(let num 0)
(let current_pid (pid process))
(while (< integer num max)
(let pid (spawn process
(lambda
; Processes are completely isolated and thus don't
; inherit any variables. This means we need to re-import
; our modules.
(import std array)
(import std process)
(import std integer)
; Wait for a message in the format of [pid, number]
(let message (receive process))
(let pid (at array message 0))
(let num (at array message 1))
; Multiply the number by 2
(let resp (* integer num 2))
; Send the number back to the sender
(send process pid resp))))
; Send the number to the "worker"
(send process pid (array current_pid num))
(let num (+ integer num 1)))
(let num 0)
(let total 0)
; Collect the output of every process.
(while (< integer num max)
(let got (receive process))
(let total (+ integer total got))
(let num (+ integer num 1)))
(print stdout (to_string integer total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment