From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Panicz Maciej Godek Newsgroups: gmane.lisp.guile.user Subject: Re: Passing objects between threads Date: Sat, 10 Sep 2016 22:16:22 +0200 Message-ID: References: <20160910153006.695295d7@bother.homenet> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: blaine.gmane.org 1473538681 16026 195.159.176.226 (10 Sep 2016 20:18:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 10 Sep 2016 20:18:01 +0000 (UTC) Cc: "guile-user@gnu.org" To: Chris Vine Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Sep 10 22:17:58 2016 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bioim-0003E4-9Q for guile-user@m.gmane.org; Sat, 10 Sep 2016 22:17:52 +0200 Original-Received: from localhost ([::1]:35559 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bioik-0004GA-CJ for guile-user@m.gmane.org; Sat, 10 Sep 2016 16:17:50 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50989) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1biohO-0003Wf-L7 for guile-user@gnu.org; Sat, 10 Sep 2016 16:16:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1biohN-0006kf-7O for guile-user@gnu.org; Sat, 10 Sep 2016 16:16:26 -0400 Original-Received: from mail-wm0-x235.google.com ([2a00:1450:400c:c09::235]:38277) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1biohM-0006kJ-RA for guile-user@gnu.org; Sat, 10 Sep 2016 16:16:25 -0400 Original-Received: by mail-wm0-x235.google.com with SMTP id 1so85040355wmz.1 for ; Sat, 10 Sep 2016 13:16:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=pU1Uo20YKbhx/a1Od/codZ2cJwfcgPYxURxPukEVlNA=; b=X4QuKLf4UT0tQodr0W38LRKQk8alCN+RGz0VBqdHGFxSWVxm8NLAyPU/4T84w3PkCf gEhZrNXMYKOWDMDXOCoE35Mwf5KQpmCaMtWTmWR6tAY+ZNXkEPjLD7LJ2Brl2g/kaXXW EXG1pm/gtY3D41moLLP8ooTbFwf1vXjzSKz/JzS0TqjT9KEpXwHSDVE138R1G3+wQTJi wpoIPZBR0W0Vyl9K9KR/Adx2kM0QrqjV4ga+X8e5ZnDho2M0T1+gNSUK/b6jXJeKxFmO pb+OclcHjmtZXemFPrKB173JTXu/1sr1oDzyEwjGABRJ1AztI8IZP7nxJAbTLh4DMoif iF3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=pU1Uo20YKbhx/a1Od/codZ2cJwfcgPYxURxPukEVlNA=; b=Gdj/SNWQXlMOSRVkivrCUpmxOs7T8o5JpnpUbC1XYf6wjypUhhwbkElSEu/Xa5OcLw N1l2CYJhelcu2yWWkc8aazu4m8i8YJkNLAXYCoASwhAqUfiBpsXNqczoWVZK5onSoIas mDDPXHHKQgu2/5/68LpnGdEKizMyp+vtqmhVxYcvXpqYQg6pUsCGLg3OiKcnk+7lmw1g cdfktSNdDIVWBm/GhOiGdPA+J+KTK/u34O8wgsDw2KmzXr6RotKl5xsHNzHQS7l2qeyC zegwbCR8im7R/CRViXIDXoFHocHdZN49Grb8Ti8DIwHX6NyloBTr9yNJSi/lZykcHs4f hKYA== X-Gm-Message-State: AE9vXwMbcBC30i6ddfOhqhwy3v38m93kB1uEuTHJAWJbzs8eBRieZbQknmuSfobVIi+vgUCPImylG+0MvDyvRA== X-Received: by 10.28.8.19 with SMTP id 19mr3635522wmi.81.1473538583056; Sat, 10 Sep 2016 13:16:23 -0700 (PDT) Original-Received: by 10.80.182.242 with HTTP; Sat, 10 Sep 2016 13:16:22 -0700 (PDT) In-Reply-To: <20160910153006.695295d7@bother.homenet> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::235 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:12898 Archived-At: I agree, it is a simple concept, and easily implementable using condition variables. However, it would probably be nicer if guile provided some standard solution, instead of forcing every programmer to coming up with their own ones. I've noticed that there's a module (ice-9 occam-channel) bundled with Guile, but unfortunately it is undocumented. Maybe it is a problem with the Guile community that -- despite some attempts like Guildhall -- we didn't manage to create anything like CPAN or pip. (I think that the Chicken community was more successful in that regard) 2016-09-10 16:30 GMT+02:00 Chris Vine : > On Sat, 10 Sep 2016 11:37:55 +0200 > Panicz Maciej Godek 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 > (_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))) > > >