Multithreading and timing with Windows API
-
Hi, Being a newbie in multithreading concept, I'd like to better understand why multithreading and/or multi-cores CPU could be problematic while determining timing in a program. Suppose, for example, that I want to know the elapsed time since a user has clicked with a mouse on the PC screen. I understand that there should be an event handler, but I read on some websites that computing such a deltatime could be very tricky, especially for multithreading and/or multi-cores PC. On the other hand, some people warns about using GetPerformanceCounter because of the multithreading and/or multi-cores... I would like to understand why and the concepts behind ? Thank you a lot,
Niaina
-
Hi, Being a newbie in multithreading concept, I'd like to better understand why multithreading and/or multi-cores CPU could be problematic while determining timing in a program. Suppose, for example, that I want to know the elapsed time since a user has clicked with a mouse on the PC screen. I understand that there should be an event handler, but I read on some websites that computing such a deltatime could be very tricky, especially for multithreading and/or multi-cores PC. On the other hand, some people warns about using GetPerformanceCounter because of the multithreading and/or multi-cores... I would like to understand why and the concepts behind ? Thank you a lot,
Niaina
The first problem is that your thread might be executed by different cores, the OS might switch your thread from one core to another. There are several problems with this: the switch takes time, it might screw up per-core cache efficiency and another problem is that
GetPerformanceCounter()
uses the tick count of the processor that said to be problematic on some systems if your thread is jumping from one core to another. To force the execution of your thread to a specific core use SetThreadAffinityMask()[^]. With this you might want to force every thread in your program to a specific core to avoid the problems I described above.