4 ms·
> Very rarely do I feel like I'm missing something in elisp, with the exception of concurrency support! I have some good news for you, then! Cooperative thre
by Paul-Craft 3y ago
> Very rarely do I feel like I'm missing something in elisp, with the exception of concurrency support!
I have some good news for you, then! Cooperative threading was added in Emacs 26:
https://www.gnu.org/software/emacs/manual/html_node/elisp/Threads.html https://www.gnu.org/software/emacs/manual/html_node/elisp/Th...
https://www.emacswiki.org/emacs/NoThreading https://www.emacswiki.org/emacs/NoThreading
I've never used it, but the big disadvantage seems to be that unless you're going to do some super advanced macrology, any function you run within a thread needs to explicitly call `thread-yield` in order to yield back to the scheduler so other threads can run. I don't know of any library or anything that makes it so you can just take a function that isn't thread-aware, run it concurrently with other possibly not thread-aware functions, and have it all work sensibly, but at least the building blocks are there.
---
If that doesn't suit you, Emacs 25 introduced generators!
https://www.gnu.org/software/emacs/manual/html_node/elisp/Generators.html https://www.gnu.org/software/emacs/manual/html_node/elisp/Ge...
Generators have some funky interactions with `unwind-protect`, so handle with care.
---
Finally, there's also a (really old and probably won't work as is anymore, but also probably fun to poke at) coroutine library available:
https://www.emacswiki.org/emacs/CoRoutines https://www.emacswiki.org/emacs/CoRoutines
---
I'm sure one of those (probably emacs threads) will meet your needs nicely. :)
- gpderetta 3y agoDidn't emacs add actual support for OS threads recentishly?
- Paul-Craft 3y agoNot to my knowledge. Apparently, according to https://www.emacswiki.org/emacs/NoThreading https://www.emacswiki.org/emacs/NoThreading, dynamic scope makes it devilishly difficult to implement os-level threading. It is, apparently, possible to automatically translate elisp into a lexically scoped lisp dialect, and rebuilt emacs on that foundation, but, again, to my knowledge, nobody's done that. And, there are occasional mutterings about making a new, natively multithreaded emacs, but nothing's ever made it into the GNU emacs repo, AFAIK. It does have process-based concurrency, which is basically where you spawn off a whole new emacs, then run your code within that. I've never used it, so I don't know how it works, other than the obvious disadvantage of having a large startup time and potentially memory use to start up another instance of emacs.
- gpderetta 3y agoYou are indeed right. I had understood that actual preemptive threads had been added, but it seems that for now it is still cooperative and requires explicit yields. Note that elisp has lexical scoping now, but it will be years of course before all packages will have been updated (and for many things you still want dynamic scoping).