Why are 50 threads faster than 4?

前端 未结 5 1628
野趣味
野趣味 2020-12-29 06:06
DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
    volatile auto x = 1;
    for (auto i = 0; i < 800000000 / MAX_THREADS;         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 06:13

    It all depends on what your threads are doing.

    Your computer can only concurrently run as many threads as there are cores in the system. This includes virtual cores via features like Hyper-threading.

    CPU-bound

    If your threads are CPU-bound, (meaning they spend the vast majority of their time doing calculations on data that is in memory), you will see little improvement by increasing the number of threads above the number of cores. You actually lose efficiency with more threads running, because of the added overhead of having to context-swtich the threads on and off the CPU cores.

    I/O-bound

    Where (#threads > #cores) will help, is when your threads are I/O-bound, meaning they spend most of their time waiting on I/O, (hard disk, network, other hardware, etc.) In this case, a thread that is blocked waiting on I/O to complete will be pulled off the CPU, and a thread that is actually ready to do something will be put on instead.

    The way to get highest efficiency is to always keep the CPU busy with a thread that's actually doing something. (Not waiting on something, and not context-switching to other threads.)

提交回复
热议问题