GNU bug report logs -
#63311
30.0.50; [PATCH] smtpmail-send-it split
Previous Next
Full log
Message #32 received at 63311 <at> debbugs.gnu.org (full text, mbox):
Eli Zaretskii <eliz <at> gnu.org> writes:
>> From: Manuel Giraud <manuel <at> ledu-giraud.fr>
>> Cc: 63311 <at> debbugs.gnu.org
>> Date: Thu, 11 May 2023 22:59:20 +0200
>>
>> (defun my-problem ()
>> (interactive)
>> (let ((buf (generate-new-buffer "*foo*")))
>> (with-current-buffer buf
>> (insert "secret message"))
>> (unwind-protect
>> (make-thread #'(lambda ()
>> (with-current-buffer buf
>> (sit-for 10)
>> (message (buffer-string)))))
>> (kill-buffer buf))))
>> --8<---------------cut here---------------end--------------->8---
>>
>> The thread won't have a chance to do its job since the buffer will
>> already be dead.
>
> Of course. So you cannot do that, obviously. Cleanup that gets in
> the way of code you run in a thread must be done after thread-join
> returns, or when thread-live-p returns nil for the thread. Or use
> some other synchronization method. There could be several such
> threads alive at the same time, btw.
>
> In the unwind-protect handler of the main thread you can only do
> cleanup of stuff that the thread doesn't need, or if you are sure the
> thread was not started (due to some error that precludes the call to
> make-thread).
Thanks! So I could go with something like this:
--8<---------------cut here---------------start------------->8---
(setq-local lexical-binding t)
(defun eli-solution ()
(interactive)
(let ((buf (generate-new-buffer "*foo*")))
(with-current-buffer buf
(insert "secret message"))
(let ((cleanup #'(lambda () (kill-buffer buf)))
thread)
(unwind-protect
(setf thread (make-thread #'(lambda ()
(with-current-buffer buf
(sit-for 10)
(message (buffer-string))
(funcall cleanup)))))
(unless (thread-live-p thread)
(funcall cleanup))))))
--8<---------------cut here---------------end--------------->8---
BTW, do you know a more elisp way of defining a function than
"(let ((f #'(lambda…))))" form?
--
Manuel Giraud
This bug report was last modified 1 year and 220 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.