Win32: Atomic Code Execution

I have two system calls GetSystemTime () and GetThreadTimes () that I need to calculate the CPU utilization of a given Win32 thread.

For accuracy, I must ensure that both GetSystemTime () and GetThreadTimes () are executed atomically; that is, there should be no context switch between calling GetSystemTime () and GetThreadTimes ().

The reason is that sometimes I get a percentage over 100% (~ 1 in 500).

How can I ensure that 2 function calls are made atomic?

Thanks, Sachin

+1


source to share


4 answers


It is best to prioritize the compute thread in real time. Real streams are only preempted by other real-time (or higher priority real-time) streams.



+2


source


I'm not sure if you can. If you don't know what the basic operations are in each function, and since they tend to be Windows API calls ... good luck.

Wouldn't it be possible to just adjust the percentages over 100?



perc = ( perc > 100.0f ) ? 100.0f : perc;

      

+2


source


Unfortunately there is no way. In between two system calls in Win32, there is no way to prevent your process / thread from switching. Otherwise, it would be trivial for someone to implement a process that would block the system by refusing every inclusion.

+1


source


Can calling functions in reverse order help? (99% is better than 101% ...)

Also, if you forcibly toggle the context switch just before it (for example, by calling Sleep ()), you probably reduce the chances of it being disabled. I don't know how well it works.

+1


source







All Articles