Win32 count increase or decrease operation of atomic operations--interlockedincrement and InterlockedDecrement

Source: Internet
Author: User

InterlockedIncrement and InterlockedDecrement

The atomic addition and subtraction of the implementation number.

What is atomic addition and subtraction?

For example, if a variable is Long value = 0;

First of all, the normal situation of the addition and subtraction operation: value+=1;

1: The system takes out the value from the space of value and dynamically generates a space to store the values taken out;

2: Add the extracted value and 1, and overwrite the original value with the space returned to value. The addition ends.


If there are two thread at this time, they are recorded as threada,threadb.

1:threada the value from the storage space, which is 0;

2:threadb the value from the storage space, which is 0;

3:threada the value to be taken out and 1 for addition, and overwrites the original value with the space returned to value. End of addition, value=1.

4:threadb the value to be taken out and 1 for addition, and overwrites the original value with the space returned to value. End of addition, value=1.

The last value = 1, and the correct should be 2; this is where the problem is, and interlockedincrement can guarantee that other threads will not be able to access the variable when one thread accesses it. similarly interlockeddecrement.

LONG InterlockedDecrement (
Lplong lpaddend//variable address
);
is an interlock function, used in the same process, need to share a variable, when doing subtraction,
Preventing other threads from accessing this variable is one way to implement thread synchronization (interlock function)

The first thing to understand is multithreading, sharing resources (and accessing global variables at the same time), otherwise it's hard to understand.

result = InterlockedDecrement (&someint)

If you do not consider multithreading is actually result = SomeInt-1;

But considering the multithreading problem is a bit more complicated. That means it's not easy to get the results I expected.

result = SomeInt-1;

For example,
Someint if ==1;
The expected result is of course ==0;

However, if Someint is a global variable that is shared throughout the whole situation it is different.
C Language "result = SomeInt-1;"
In the actual execution process, there are a few instructions, during the execution of the instruction, other threads may change the Someint value, so that the real result and your expected inconsistency.

So the execution of InterlockedDecrement (&someint) is like this
{
__ prohibit other threads from accessing (&someint) this address

Someint--;

Move EAX, Someint; Set the return value, and the return value of the C + + function is placed in EAX.

__ Open Other thread access (&someint) this address
}

But in fact, only a few instructions to add a prefix can be completed, the above description is magnified.

You might say, is this necessary? In general, the probability of an error is small, but prevention is always necessary
If multithreading is not considered
result = InterlockedDecrement (&someint);
is result = SomeInt-1;
If Someint==1,result must be ==0;

However, in multi-threading, if Someint is a global variable shared between threads, the situation is less straightforward.
result = SomeInt-1;
In the CPU, several instructions are executed. In the middle of the instruction it is possible that Someint is modified by thread. The actual result is not the result you expected.

InterlockedDecrement (&someint)
The process of amplification is as follows:
{
__ prohibit other threads from accessing &someint address;

Someint--;

Other threads do not modify the Someint value here.!!!!!!

mov EAX, someint; The C + + function return value is always placed in eax.

__ Open Other threads to access the &someint address;
}

The actual CPU execution process has only a few prefixed instructions (586 instructions)

Would you say, is it necessary? There is little chance of error, but mistakes always need to be prevented. Of course can be implemented with other multi-threading mechanism, but not so concise, so interlocked ... function is necessary to provide.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.