Книга: 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.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.061. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз