> Date: Tue, 28 Jan 2025 15:15:19 +0200
> From: Eli Zaretskii
> To: "Yue Yi"
> Can you describe in short how this will work from the thread
> management POV? E.g., when (under what circumstances) will the
> threads be created, and will they ever exit or be killed? Our needs
> are quite unique, I think, so perhaps it would make sense to have some
> special thread management policies (as opposed to what standard thread
> pool implementations provide)? Not that I'm an expert on thread
> pools, so maybe what I say here makes little sense.
Sure.
A thread pool is a collection of pre-initialized threads that are ready
to perform tasks. Instead of creating and destroying threads for each
task, a thread pool reuses a fixed number of threads to handle multiple
tasks. This improves performance and resource efficiency by reducing the
overhead of thread creation and destruction.
For Emacs, my current implementation involves creating an array of
structures with a length of 32, where each element contains contextual
information for a thread. Threads are only created when necessary, such
as when the number of child processes exceeds 32 (or more) for the first
time. Once created, the threads do not exit; instead, they enter an
infinite loop, waiting for an event to trigger and begin their
WaitForMultipleObjects tasks.
Once a thread finishes waiting, it notifies the main thread, and the
main thread then instructs the other child threads to stop
waiting (Another possible situation is that the main thread completes its
waiting first and then directly notifies all the child threads.). Then,
they begin waiting for the next task of calling WaitForMultipleObjects.
In this implementation, when there are a large number of child processes
(around 1000), the maximum call time for accept-process-output does not
exceed 100 microseconds. However, there might still be room for
improvement.
By the way, can I use _WIN32_WINNT to check the current Windows version
and make the most of some system features, such as the thread pool? I
will compare the performance of the system thread pool with my own
implementation to decide whether to use them.
Regards.