The Interlocked Functions

< BACK  NEXT >
[oR]

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

  • InterlockedIncrement Increment an integer variable. The function takes a single parameter, which is a pointer to the integer to increment.

  • InterlockedDecrement Decrement an integer variable. The function takes a single parameter, which is a pointer to the integer to increment.

  • InterlockedExchange Places a new value into an integer variable. The first parameter is a pointer to an integer to receive the new value, and the second parameter is the new integer value.

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.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net