help-gnu-emacs
[Top][All Lists]
Advanced

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

Setting buffer and point from inside thread


From: Ihor Radchenko
Subject: Setting buffer and point from inside thread
Date: Sat, 10 Apr 2021 15:05:31 +0800

Hi,

I am trying to play with threading functionality.
In my use-case, thread function depends on point position and current
buffer.  Since, user will most likely move the point around in the main
thread, I was trying to find a way to preserve current buffer and point
position for the thread. I came up with the following test code in my
*scratch*:

(make-thread
 (lambda ()
   (while t
     (sleep-for 0.2)
     (message "Buffer: %s:%s" (current-buffer) (point))
     (thread-yield-safe)
     )))

(defun thread-yield-safe (&rest _)
  "Call `thread-yield' preserving buffer and point."
  (let ((buf-- (current-buffer))
        (mk-- (point-marker)))
    (thread-yield)
    (set-buffer buf--)
    (goto-char mk--)))

In theory, buffer and point in message should not change as the thread
is running. However, after C-x C-e running the thread, the point is not
always preserved. The behaviour is actually very strange. Sometimes, the
point is preserved, sometimes not. Sometimes, when I am trying to open
new buffer it somehow becomes read-only. Sometimes, the point is moved
in the new buffer. Once, emacs even crashed.

Clearly, many things are going wrong in this code and Emacs should not
behave so strangely. Could someone knowledgeable explain if I am doing
something wrong. Or maybe it is Emacs bug?

Best,
Ihor



reply via email to

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