December 31st, 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.
4 Comments |
VC++ |
Permalink
Posted by Shibu
December 30th, 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.
No Comments » |
VC++ |
Permalink
Posted by Shibu
December 19th, 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;
}
No Comments » |
VC++ |
Permalink
Posted by Shibu