[Top][All Lists]

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

Re: make-thread with lambda form instead of function symbol

From: Eric Abrahamsen
Subject: Re: make-thread with lambda form instead of function symbol
Date: Sun, 16 Apr 2017 11:11:29 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Noam Postavsky <address@hidden> writes:

> On Sun, Apr 16, 2017 at 12:05 PM, Eric Abrahamsen
> <address@hidden> wrote:
>> I'm trying to do something that seems like it would be a normal use
>> case: spawn a series of threads which call the same function using
>> different external processes. Practically what this means is that I want
>> to pass a function-plus-argument form to make-thread, not a function
>> symbol. Something like:
>> (let* ((results)
>>        (sources '(source1 source2))
>>        (threads
>>         (mapcar
>>          (lambda (s)
>>            (make-thread
>>             (funcall
>>              (lambda ()
>>                (push (get-stuff-from-source s) results)))))
>>          sources)))
>>   (mapc #'thread-join threads)
>>   results)
>> The (funcall (lambda () thing was the only way I could get anything but
>> nil out of the thread functions. I think I'm fooling myself, though: so
>> far as I can tell, `get-stuff-from-source' is fully evaluated before the
>> thread is made, and nothing at all happens during the #'thread-join
>> loop.
> Just drop the funcall: (make-thread (lambda () ...)) should do what
> you want, assuming you have lexical-binding set (if not, you can
> construct a lambda-list with backquote or similar).

Thanks for the response. The first thing I learned here was that
lexical-binding was nil in *scratch*, but t in the source file I'm
writing -- not a great testing setup.

I'm still not seeing the results I expect, though. Here's a dumb test:

(setq lexical-binding t)

(let ((threads
        (lambda (el)
           (lambda ()
             (push (cl-incf el) results))))
        '(1 2 3)))
  (mapc #'thread-join threads)

This gives me nil.

(Incidentally, if I put this in a function and edebug it, it tells me
edebug will stop at the next break point, and then enters a level of
recursive editing I can't escape from: C-M-c gives me "No catch for tag:
exit, nil".)

Should the above example work?

Thanks again,

reply via email to

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