Subject: 28.1.90; An idea to allow background (low-priority) threads

From: Ihor Radchenko <[email-masked]>
Date: Sun, 14 Aug 2022 12:12:20 +0800

Hi,

Emacs does have a limited concurrency support via Threads, which is,
unfortunately, mostly a toy feature I haven't seen being used a lot.

All my attempts to implement some real functionality using threads
failed because it is very hard to create real-life threads that do not
noticeably block Emacs. Most of the time, Emacs gets quite sluggish,
even if the thread yields frequently.

Such situation sounds similar to what one may get with 1CPU and
multiple processed fighting for the CPU time. This is something that
used to be solved with nice command.

Could something like nice be implemented for Elisp threads?

Or, alternatively, could thread execution be limited in some ways?
I have the approach taken by org-element cache processing in mind.
org-element limits cache processing using the following variables:

(defvar org-element-cache-sync-idle-time 0.6
  "Length, in seconds, of idle time before syncing cache.")

(defvar org-element-cache-sync-duration 0.04
  "Maximum duration, as a time value, for a cache synchronization.
If the synchronization is not over after this delay, the process
pauses and resumes after `org-element-cache-sync-break'
seconds.")

(defvar org-element-cache-sync-break 0.3
  "Duration, as a time value, of the pause between synchronizations.
See `org-element-cache-sync-duration' for more information.")

I imagine that something similar could be done for threads.
`make-thread' could allow some kind of priority setting that will limit
its execution time to comfortable levels, so that the user in main
thread can actually interact with Emacs without noticing anything.

WDYT?

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92