L
L
low molecular macro2020-11-18 23:20:31
Node.js
low molecular macro, 2020-11-18 23:20:31

How does Thread Pool work?

I'm trying to understand how multithreading works in Node. And if the work of the Event Loop in many videos is explained in sufficient detail, then the work of the Thread Pool is always mentioned in passing (at least in all those videos that I watched). For me, the following points remain incomprehensible:
1) what is this thread of yours anyway? If the Event Loop throws a task into the Thread Pool before the end of execution, then where / what / how is this thread executed?
2) it is said that there can be from 4 to 128 threads. And it is 4 threads that are usually enough for the healthy life of an average such application. Why is the number of threads limited to 128?
3) states that Node is not well suited for heavy operations such as cryptography, AI, and the like. Such operations can stall the server, as the Thread Pool will be busy performing these "heavy" operations for much longer than expected. But aren't threads designed just to perform "heavy" operations, unlike the Event Loop? And after all, it can turn out that all 4 threads (which, as they say, are usually enough) will be busy writing or reading a large file?
4) Does the number of threads affect performance? Why not set the default maximum (128) number of threads instead of 4? Just in case...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2020-11-20
@molekulyarniy

1) The difference between processes (process) and threads (t... . If very roughly - the thread is "simpler" and "cheaper" than a full-fledged process.
2) Already up to 1024 ( https://docs.libuv.org/en/v1 .x/threadpool.html ). This is a limitation of libuv, not the operating system. The number 4 is simply taken from the ceiling and justified empirically.
3)
> But aren't threads designed just to perform "heavy" operations, in contrast to the Event Loop?
Calculated, without them everything would immediately fall apart, but on average everything works with them. But if you need a number crusher, then it's better to look at other PLs.
> And after all, it may turn out that all 4 threads will be busy
, yes, maybe it will be bad.
4)
> whether the number of threads affects performance
Not by itself, but it doesn't make much sense to make them (much) more than the number of processor cores. Anyway, in the end, everything will be done by the processor, and if you have 4 cores and 128 threads, then the processor, instead of computing, will be doing meaningless context switching.
Well, do not forget that threads, although “lighter” than processes, are still not free.
Z.Y. all of the above is a very rough description of reality

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question