How to Increment/Decrement A Variable In A Thread Safe Manner

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).

For example,

LONG myNo = 10;
LONG returnValue = InterlockedIncrement(&myNo);

will make

myNO = 11 and returnValue = 11.


How To Get The Time Elapsed Since The System Was Started

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.


How To Get the Processor Speed

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.

DWORD GetProcessorSpeed()
{
   HKEY hKey;
   // Read the speed from registry
   const long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                      "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
                      0,
                      KEY_READ,
                      &hKey);

   DWORD dwMHz = 0;
   DWORD BufSize = MAX_PATH;
   if (ERROR_SUCCESS == error)
   {
      RegQueryValueEx(hKey, "~MHz", 0, 0, (LPBYTE)&dwMHz, &BufSize);
   }
   return dwMHz;
}

How To Get The Current CPU Cycle Count

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.

 
__int64 GetCpuCycle()
{
   unsigned int LowWord = 0;
   unsigned int HighWord = 0;
   _asm
   {
      // Insert Real Time Stamp Counter opcodes
      _emit 0x0F
      _emit 0x31
      mov HighWord, edx
      mov LowWord, eax
   }
   return ((__int64)(HighWord) << 32) + LowWord; } [/sourcecode] We can use the above function at the beginning and end of a program to get the no. of clock cycles it needed.


Shortcut keys in Visual studio 2005

December 5, 2008

The folowing is a description of some of the shortcut keys in Visual Studio 2005 (generously contributed by friend Premraj).

Ctrl-Alt-V, A
Displays the Auto window to view the values of variables currently in the scope of the current line of execution within the current procedure
Ctrl-Alt-C
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
Ctrl-Alt-D
Displays the Disassembly window
Ctrl-Alt-I
Displays the Immediate window, where you can evaluate expressions and execute individual commands
Ctrl-Alt-V, L
Displays the Locals window to view the variables and their values for the currently selected procedure in the stack frame
Ctrl-Alt-M, 1
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
Ctrl-Alt-M, 2
Displays the Memory 2 window
Ctrl-Alt-M, 3
Displays the Memory 3 window
Ctrl-Alt-M, 4
Displays the Memory 4 window
Ctrl-Alt-U
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
Ctrl-Alt-G
Displays the Registers window, which displays CPU register contents
Ctrl-Alt-N
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
Ctrl-Alt-V, T
Displays the This window, which allows you to view the data members of the object associated with the current method
Ctrl-Alt-H
Displays the Threads window to view all of the threads for the current process
Ctrl-F11
Displays the disassembly information for the current source file. Available only in break mode
Ctrl-Alt-W, 1
Displays the Watch 1 window to view the values of variables or watch expressions
Ctrl-Alt-W, 2
Displays the Watch 2 window
Ctrl-Alt-W, 3
Displays the Watch 3 window
Ctrl-Alt-W, 4
Displays the Watch 4 window