From: Chris Vine <chris@cvine.freeserve.co.uk>
To: guile-user@gnu.org
Subject: Re: Passing objects between threads
Date: Sat, 10 Sep 2016 15:30:06 +0100 [thread overview]
Message-ID: <20160910153006.695295d7@bother.homenet> (raw)
In-Reply-To: <CAMFYt2YKuh0O_bNULu7UwdJE1SU6TGcNkPvkxrw049Yc2sJtTQ@mail.gmail.com>
On Sat, 10 Sep 2016 11:37:55 +0200
Panicz Maciej Godek <godek.maciek@gmail.com> wrote:
> Hi,
> is there any easy way to create a channel (queue) that could be used
> to communicate between threads? In particular, if the queue is empty,
> I would like the consumer to wait until something appears in it
> (pretty much like the channels in Clojure)
I haven't used Clojure channels, but what you describe is a traditional
blocking asynchronous queue. Since guile has POSIX condition variables
they are trivial to make. Here is one simple implementation, which
allows multiple writers and multiple readers. async-queue-pop! blocks a
reader until something is available in the queue. If there are
multiple readers, which reader awakens on any write is for the thread
scheduler. A loop is included in async-queue-pop! because POSIX
condition variables can spuriously wake up, so you have to test the
condition (whether the queue is empty or not) on any wake up before
returning.
You will find loads of literature and tutorials on this pattern, using
condition variables.
******************************************
(use-modules (srfi srfi-9)
(ice-9 q)
(ice-9 threads))
(define-record-type <async-queue>
(_make-async-queue mutex cond q)
async-queue?
(mutex mutex-get)
(cond cond-get)
(q q-get))
(define (make-async-queue)
(_make-async-queue (make-mutex)
(make-condition-variable)
(make-q)))
(define (async-queue-pop! a-q)
(let ([mutex (mutex-get a-q)])
(with-mutex mutex
(let ([q (q-get a-q)])
(let loop ()
(if (q-empty? q)
(begin
(wait-condition-variable (cond-get a-q) mutex)
(loop))
(deq! q)))))))
(define (async-queue-push! a-q item)
(with-mutex (mutex-get a-q)
(enq! (q-get a-q) item))
(signal-condition-variable (cond-get a-q)))
next prev parent reply other threads:[~2016-09-10 14:30 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-10 9:37 Passing objects between threads Panicz Maciej Godek
2016-09-10 10:35 ` Diogo F. S. Ramos
2016-09-10 11:18 ` Chaos Eternal
2016-09-10 19:30 ` Panicz Maciej Godek
2016-09-10 19:29 ` Panicz Maciej Godek
2016-09-10 14:30 ` Chris Vine [this message]
2016-09-10 20:16 ` Panicz Maciej Godek
2016-09-10 21:36 ` Chris Vine
2016-09-11 18:34 ` Andy Wingo
2016-09-12 19:58 ` Panicz Maciej Godek
2016-09-12 23:10 ` Chris Vine
2016-09-15 19:37 ` Panicz Maciej Godek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160910153006.695295d7@bother.homenet \
--to=chris@cvine.freeserve.co.uk \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).