all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Manuel Giraud via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 63311@debbugs.gnu.org
Subject: bug#63311: 30.0.50; [PATCH] smtpmail-send-it split
Date: Fri, 12 May 2023 08:24:36 +0200	[thread overview]
Message-ID: <87h6si5aor.fsf@ledu-giraud.fr> (raw)
In-Reply-To: <83pm763y3r.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 12 May 2023 08:41:44 +0300")

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Manuel Giraud <manuel@ledu-giraud.fr>
>> Cc: 63311@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





  reply	other threads:[~2023-05-12  6:24 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-05 13:13 bug#63311: 30.0.50; [PATCH] smtpmail-send-it split Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-05 18:57 ` Eli Zaretskii
     [not found]   ` <874joq34bk.fsf@ledu-giraud.fr>
2023-05-06  6:20     ` Eli Zaretskii
2023-05-06  9:34       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-09  9:52       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-10 11:47         ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-11 13:28           ` Eli Zaretskii
2023-05-11 20:59             ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-05-12  5:41               ` Eli Zaretskii
2023-05-12  6:24                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-05-12  7:09                   ` Eli Zaretskii
2023-05-12  7:57                     ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-01 12:35                       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-01 12:59                         ` Eli Zaretskii
2023-11-01 18:06                           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-01 19:29                             ` Eli Zaretskii
2023-11-01 19:55                               ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-02  5:48                                 ` Eli Zaretskii
2023-11-02 10:44                                   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-02 11:26                                     ` Eli Zaretskii
2023-11-05 14:24                                       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-05 15:46                                         ` Eli Zaretskii
2023-11-05 16:12                                           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-05 16:17                                             ` Eli Zaretskii
2023-11-06 10:21                                               ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-06 12:40                                                 ` Eli Zaretskii
2023-11-06 15:56                                                   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-06 16:28                                                     ` Eli Zaretskii
2023-11-06 17:57                                                       ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-06 19:21                                                         ` Eli Zaretskii
2023-11-06 20:55                                                           ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-07  3:26                                                             ` Eli Zaretskii
2023-11-07  9:06                                                               ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-07 12:12                                                                 ` Eli Zaretskii
2023-11-07 12:56                                                                   ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-07 13:17                                                                     ` Eli Zaretskii
2023-11-03  2:31                             ` Richard Stallman
2023-11-03  7:18                               ` Eli Zaretskii
2023-11-03 13:49                                 ` Manuel Giraud via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-03 14:29                                   ` Eli Zaretskii
2023-05-12  7:10                   ` Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h6si5aor.fsf@ledu-giraud.fr \
    --to=bug-gnu-emacs@gnu.org \
    --cc=63311@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=manuel@ledu-giraud.fr \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.