Ensuring protected access to global integer values turns out to be a common requirement in many applications, so the Windows CE API provides the "interlocked" functions to allow safe incrementing, decrementing, and swapping of values in global integer variables. The functions are
For example, the following code declares a global integer value and uses InterlockedIncrement to increment the value in that variable. Using this function ensures that other functions using the interlocked functions will block until this call is completed. LONG g_lMyVar; InterlockedIncrement(&g_lMyVar); Note that all changes to the variable g_lMyVar should be through the interlocked functions to ensure that correct synchronization occurs. If you need to perform a more complex calculation (for example, one that involves multiplication that does not have an interlocked function), you should perform the calculation using local variables, and then copy the value into the global integer variable using the InterlockedExchange function.
|