Книга: Microsoft Windows Embedded CE 6.0 Exam Preparation Kit
Interlocked Functions
Interlocked Functions
In multithread environments, threads can be interrupted at any time and resumed later by the scheduler. Portions of code or applications resources can be protected using semaphores, events, or critical sections. In some applications, it could be too time consuming to use those kinds of system objects to protect only one line of code like this:
// Increment variable
dwMyVariable = dwMyVariable + 1;
The sample source code above in C is one single instruction, but in assembly it could be more than that. In this particular example, the thread can be suspended in the middle of the operation and resumed later, but errors can potentially be encountered in the case of another thread using the same variable. The operation is not atomic. Fortunately, it is possible in Windows Embedded CE 6.0 R2 to increment, decrement, and add values in multithreading-safe, atomic operations without using synchronization objects. This is done by using interlocked functions.
Table 3-15 lists the most important interlocked functions that are used to atomically manipulate variables.
Table 3-15 Interlock API
Function | Description |
---|---|
InterlockedIncrement | Increment the value of a 32 bit variable. |
InterlockedDecrement | Decrement the value of a 32 bit variable. |
InterlockedExchangeAdd | Perform atomic addition on a value. |
- CHAPTER 4 Functions and Libraries in mikroC
- Functions
- Basic Functions
- Using Functions in Shell Scripts
- 4.1 mikroC Functions
- 4.2 mikroC Built-in Functions
- 4.3 mikroC Library Functions
- 4.1.2 Passing Arrays to Functions
- 4.1.3 Passing Variables by Reference to Functions
- 7.2 mikroC Language SD Card Library Functions
- 8.6 mikroC Language USB Bus Library Functions
- 9.10 mikroC CAN Functions