Thread costs
Threads are kernel-level constructs, so each operation on them requires a context switch from the user land to the kernel context (also known as a kernel trip). This is rather expensive because we are changing the privilege level of code and also switching all the context data of the current thread. An often-given estimate for crossing the kernel boundary in a syscall is 1,000-1,500 CPU cycles. By comparison, the dreaded reading from main RAM needs 100-150 CPU cycles.
One big cost, which we do not want to pay too often, is the thread creation and deletion overhead. Creating and deleting threads each time we have some operation to be run in parallel with another would be a great waste of resources. This problem can be solved using ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access