views:

221

answers:

2

Under windows, is there anyway to programmatically count context switches of the same process? the best thing is a callback that gets called whenever a thread is switched.

+5  A: 

There's a performance counter that does the work for you. All you have to do is read its value. You can find a description on how to do it interactively here, but performance counters can also be consumed using their API.

On Freund
Yonatan Karni
yes, this should work. it'll use that, thanks all!
yigal
+1  A: 

Problem with counting your own context switches is that you might be switching contexts while counting them! Worse, your own counting code will subtract from the amount of time your own process has, thus you can execute less within one context cycle.

As "On Freund" (+1) says, use the performance counter instead, which counts contexts at a higher level.

Workshop Alex