I create N threads using pthread_create on Linux/gcc. Each thread writes using for (;;) printf(...) its ID as fast as it can, nothing else. I let the whole program run for 3 seconds using usleep and notice that on my 4-core CPU when N=4 it produces something around 1,000,000 lines and when N=8 it produces around 4 times as much. What is the reason that on 4-core CPU 8 threads perform better? CPU doesn’t have hyperthreading enabled.
I create N threads using pthread_create on Linux/gcc. Each thread writes using for (;;)
Share
I/O and CPU operations are very different. If you run CPU-intensive computations, N=number of cores will be optimal. For I/O, the optimal number can be far higher.