Discussion:
Real time interrupt via MFC possible?
(too old to reply)
dave
2011-11-03 23:44:02 UTC
Permalink
I would like to examine the value of some counters in a long-running
program but without putting any code actually in the count loop!
Is it possible, somehow, via MFC to get an interrupt to collect those
counter values and display them in a dialog box - while the program
continues to run. [Hope that reads ok...]
If not a few tips where to start this appreciated - or mabye there is
an easier way (I hope).
Thanks
ScottMcP [MVP]
2011-11-04 03:32:15 UTC
Permalink
Interrupts are out of the question, either with or without MFC.
Interrupts are not available to user mode program in Windows.

However, you can certainly create a secondary thread within your
program, and it can examine any variables in the same program. But
there could be a synchronization issue, where one thread is in the
midst of changing a variable at the same time the other thread looks
at it. So if you can't or won't put any synchronizing code in the
count loop this technique will only work with simple types (int), not
complex types (strings, structs).
dave
2011-11-04 22:46:46 UTC
Permalink
On Thu, 3 Nov 2011 20:32:15 -0700 (PDT), "ScottMcP [MVP]"
Post by ScottMcP [MVP]
Interrupts are out of the question, either with or without MFC.
Interrupts are not available to user mode program in Windows.
However, you can certainly create a secondary thread within your
program, and it can examine any variables in the same program. But
there could be a synchronization issue, where one thread is in the
midst of changing a variable at the same time the other thread looks
at it. So if you can't or won't put any synchronizing code in the
count loop this technique will only work with simple types (int), not
complex types (strings, structs).
Thanks - at least I won't be going in the wrong direction now.
Mark Robinson
2011-11-09 14:07:04 UTC
Permalink
Post by ScottMcP [MVP]
count loop this technique will only work with simple types (int),
I think even simple types can be risky. You're just about OK if only one
thread changes the value and any other thread(s) only read it, but
something like:

int shared_int = 0;

UINT Thread1(LPVOID)
{
shared_int++;
}

UINT Thread2(LPVOID)
{
shared_int++;
}

isn't guaranteed to work as expected if Thread1 and Thread2 run
concurrently.

For this sort of thing, you need the Interlocked... set of functions.

Cheers

mark-r
Nobody
2011-11-10 21:33:25 UTC
Permalink
Post by dave
I would like to examine the value of some counters in a long-running
program but without putting any code actually in the count loop!
Is it possible, somehow, via MFC to get an interrupt to collect those
counter values and display them in a dialog box - while the program
continues to run. [Hope that reads ok...]
If not a few tips where to start this appreciated - or mabye there is
an easier way (I hope).
Besides what others suggested, I think CPU's have hardware debugging
features that can detect any changes to a any region in memory(say 4 bytes,
etc) transparently and without any overhead. If these counters or flags
don't change often, then maybe that's a good way to see when they change,
but once the change happens, it breaks into the debugger and extra CPU
cycles are used. I am guessing the debugger in VC supports this, but you can
always use an external debugger that does that. Many of us however use code
like this:

// Show progress every 1000 loops
if (Counter%1000==0) {
// Display Counter value
}

Loading...