How to estimate the thread context switching overhead?

前端 未结 9 616
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 19:29

I am trying to improve the performance of the threaded application with real-time deadlines. It is running on Windows Mobile and written in C / C++. I have a suspicion that

9条回答
  •  死守一世寂寞
    2020-11-30 19:33

    The problem with context switches is that they have a fixed time. GPU's implemented 1 cycle context switch between threads. The following for example can not be threaded on CPU's:

    double * a; 
    ...
    for (i = 0; i < 1000; i ++)
    {
        a[i] = a[i] + a[i]
    }
    

    because its time of execution is much less than context switch cost. On Core i7 this code takes around 1 micro second (depends on the compiler). So context switch time does matter because it defines how small jobs can be threaded. I guess this also provides a method for effective measurement of context switch. Check how long does the array (in the upper example) has to be so that two threads from thread pool will start showing some real advantage in compare to a single threaded one. This may easily become 100 000 elements and therefore the effective context switch time would be somewhere in the range of 20us within the same app.

    All the encapsulations used by the thread pool have to be counted to the thread switch time because that is what it all comes down to (at the end).

    Atmapuri

提交回复
热议问题