Concurrent Hello World

Below is another example of Concurrent Hello World. The macros receive and run are just 9 lines each, given below the fold.

(defpackage :example (:use :cl :node))
(in-package :example)

(defun hello (hello-queue world-queue)
  (receive hello-queue
    (:say-hello (n)
      (cond ((plusp n)
             (format t "Hello ")
             (send world-queue :say-world n))
             (send world-queue :quit)

(defun world (world-queue hello-queue)
  (receive world-queue
    (:say-world (n)
      (format t "World!~%")
      (send hello-queue :say-hello (- n 1)))
    (:quit ()

(defun main (n)
  (let ((hello-queue (make-queue))
        (world-queue (make-queue)))
      (make-node 'hello hello-queue world-queue)
      (make-node 'world world-queue hello-queue)
      (send hello-queue :say-hello n))))

(defpackage :node
  (:use :cl :alexandria :lparallel.kernel :lparallel.queue)
  (:export :send :receive :run :make-node :make-queue))
(in-package :node)

(defun send (queue label &rest args)
  (push-queue (cons label args) queue))

(defmacro receive (queue &body clauses)
  (once-only (queue)
    (with-gensyms (label args)
      `(loop (destructuring-bind (,label . ,args) (pop-queue ,queue)
               (case ,label
                 ,@(loop :for (clause-label params . body) :in clauses
                         :collect `(,clause-label
                                    (destructuring-bind ,params ,args

(defmacro run (&body body)
  (with-gensyms (channel task-count)
    `(let ((,channel (make-channel))
           (,task-count 0))
       (flet ((make-node (name &rest args)
                (apply 'submit-task ,channel name args)
                (incf ,task-count)))
         (multiple-value-prog1 (progn ,@body)
           (loop :repeat ,task-count :do (receive-result ,channel)))))))
This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Concurrent Hello World

  1. Nice example. Just one thing: where does `make-queue` come from?

  2. lparallel says:

    Hi, make-queue is in the lparallel.queue package. It’s re-exported by the node package.

  3. With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My site has a lot of unique content
    I’ve either created myself or outsourced but it seems a
    lot of it is popping it up all over the web without my authorization. Do you know any techniques
    to help prevent content from being stolen? I’d really appreciate it.

  4. What’s up, every time i used to check web site posts here
    early in the morning, because i enjoy to learn more and more.

  5. Wow, wonderful weblog format! How lengthy have you
    been blogging for? you made blogging glance easy. The entire look of your web site is wonderful, as neatly as the content material!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s