How efficient are threads under Linux?
• (REG) Incredibly. Compared with all the other kernel-based thread implementations, Linux is probably the fastest. Each thread takes only 8 kiB of kernel memory for the stack and thread creation and context switching is very fast. I have measured less than 1 microsecond context switch times on an old Pentium/MMX 200 (see http://www.atnf.csiro.au/~rgooch/benchmarks/linux-scheduler.html for more details). However, the Linux scheduler is designed to work well with a small number of running threads. Best results are obtained when the number of running theads equals the number of processors. Avoid the temptation to create large numbers of threads in your application. Threads should only be used to take advantage of multiple processors or for specialised applications (i.e. low-latency real-time), not as a way of avoiding programmer effort (writing a state machine or an event callback system is quite easy). A good rule of thumb is to have up to 1.5 threads per processor and/or one thread per