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.
4 Comments |
C++, VC++ 2005 | Tagged: Thread, Thread safe, Thread safe decrement, thread safe increment |
Permalink
Posted by cppkid
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.
Leave a Comment » |
C++, VC++ 2005 | Tagged: Get time, get time since computer was started, GetTickCount |
Permalink
Posted by cppkid
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;
}
Leave a Comment » |
C++, VC++ 2005 | Tagged: cpu speed, processor speed, registry c++, speed of processor |
Permalink
Posted by cppkid