December 31, 2008
Sometimes it will be necessary to increment or decrement a variable in a atomic manner so that no other thread can affect it while being incremented/decremented. The complex mutexes and critical sections can do the trick. Still, there is a single line API which does the same and it is
LONG InterlockedIncrement(LONG volatile* Addend)
(InterlockedDecrement for decrementing the value).
LONG myNo = 10;
LONG returnValue = InterlockedIncrement(&myNo);
myNO = 11 and returnValue = 11.
December 30, 2008
The function GetTickCount() (defined in WinBase.h) returns the no. of milliseconds elapsed since the computer was started.
The returned value is DWORD. So the above call may malfunction if the system has been up continuously for 49.7 days (as DWORD is only 32 bit). You can use the function GetTickCount64(), which returns ULONGLONG, to overcome this problem.
December 19, 2008
The processor speed in MHz can be read directly from the registry location HARDWARE\DESCRIPTION\System\CentralProcessor. The following sample code illustrates this.
// Read the speed from registry
const long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
DWORD dwMHz = 0;
DWORD BufSize = MAX_PATH;
if (ERROR_SUCCESS == error)
RegQueryValueEx(hKey, "~MHz", 0, 0, (LPBYTE)&dwMHz, &BufSize);
December 18, 2008
With a little bit of help from assembler, we can find the exact CPU cycle count. This can be very much helpful to find the execution time of a program or function. The code to find the cpu cycle count is given below.
unsigned int LowWord = 0;
unsigned int HighWord = 0;
// Insert Real Time Stamp Counter opcodes
mov HighWord, edx
mov LowWord, eax
return ((__int64)(HighWord) << 32) + LowWord;
We can use the above function at the beginning and end of a program to get the no. of clock cycles it needed.
December 5, 2008
The folowing is a description of some of the shortcut keys in Visual Studio 2005 (generously contributed by friend Premraj).
Displays the Auto window to view the values of variables currently in the scope of the current line of execution within the current procedure
Displays the Call Stack window to display a list of all active procedures or stack frames for the current thread of execution. Available only in break mode
Displays the Disassembly window
Displays the Immediate window, where you can evaluate expressions and execute individual commands
Displays the Locals window to view the variables and their values for the currently selected procedure in the stack frame
Displays the Memory 1 window to view memory in the process being debugged. This is particularly useful when you do not have debugging symbols available for the code you are looking at. It is also helpful for looking at large buffers, strings, and other data that does not display clearly in the Watch or Variables window
Displays the Memory 2 window
Displays the Memory 3 window
Displays the Memory 4 window
Displays the Modules window, which allows you to view the .dll or .exe files loaded by the program. In multiprocess debugging, you can right-click and select Show Modules for all programs
Displays the Registers window, which displays CPU register contents
Displays the Running Documents window that displays the set of HTML documents that you are in the process of debugging. Available in break and run modes
Displays the This window, which allows you to view the data members of the object associated with the current method
Displays the Threads window to view all of the threads for the current process
Displays the disassembly information for the current source file. Available only in break mode
Displays the Watch 1 window to view the values of variables or watch expressions
Displays the Watch 2 window
Displays the Watch 3 window
Displays the Watch 4 window