emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Return value of finished threads


From: Noam Postavsky
Subject: Re: Return value of finished threads
Date: Fri, 20 Jul 2018 12:17:46 -0400

On 20 July 2018 at 10:56, Michael Albinus <address@hidden> wrote:

> What I'm missing is a simple possibility to collect the return values of
> the respective finished threads (`find-file-noselect' calls), which is a
> buffer or a list of buffers. The documentation recommends the use of
> global variables, which would be inconvenient for many threads to supervise.

> What do people think?

I think you are looking for something along the lines of
promises/futures. Maybe thunk.el can be used for this (note, if trying
*scratch* remember to set lexical-binding):

;; -*- lexical-binding: t -*-
; (setq lexical-binding t) ; for *scratch*

(require 'thunk)
(defmacro thunk-delay-in-thread (&rest body)
  (declare (debug body))
  (let ((result-var (make-symbol "result")))
    `(let* ((,result-var nil)
            (thread (make-thread
                     (lambda ()
                       (setq ,result-var ,(macroexp-progn body))))))
       (thunk-delay (progn (thread-join thread) ,result-var)))))

(let ((thunks
       (mapcar
        (lambda (x) (thunk-delay-in-thread
                     (message "computing square of %d" x) (* x x)))
        (number-sequence 1 10))))
  ;; Run this thread first, just to make things more interesting.
  (thunk-force (nth 5 thunks))
  ;; Get all the values.
  (mapcar #'thunk-force thunks))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]