Techniques for atomic manipulation using interlocked in C # _c# Tutorials

Source: Internet
Author: User

What is atomic operation?

Atom (Atom) is meant to be "the smallest particle that cannot be further divided", whereas atomic manipulation (atomic operation) means "one or a series of operations that cannot be interrupted". When there are multiple threads in C # that operate on a variable at the same time, we should use atomic operations to prevent the value of multithreading from being not the most recent value.

For example: int result = 0;

Multithreading A is executing result (0) +1

Multithreading b Simultaneous execution of result (0) +1

So the final result is 1 or 2, which is hard to say. If 2 threads are computed at the same time on the CPU, then the result is 1, which is obviously not what we want. Of course you can use lock locks to ensure the uniqueness of multithreaded execution, but its performance is far less than the way the atom operates.

To use interlocked for atomic operations:

Use. NET provides a interlocked class that can perform atomic operations on some data, seemingly like a lock lock, but it is not a lock lock, its atomic operation is based on the CPU itself, non-blocking, and therefore more efficient than lock.

The following is a demonstration of atomic operations using C # code:

Class program
{
//global variable
private static int _result;
Main method
static void Main (string[] args)
{
//Run and hold the ENTER key for a few seconds, comparing the use of interlocked.increment (ref _result); result++ the different while
(true)
{
task[] _tasks = new task[100];
int i = 0;
for (i = 0; i < _tasks. Length; i++)
{
_tasks[i] = Task.Factory.StartNew ((num) =>
{
var taskid = (int) num;
Work (TaskID);
}, I);
}
Task.waitall (_tasks);
Console.WriteLine (_result);
Console.readkey ();
}
The thread calls the method
private static void Work (int TaskID)
{for
(int i = 0; i < i++)
{
//_result++;
   interlocked.increment (ref _result);}}

The above code runs separately to annotate the last two lines of code _result++; and interlocked.increment (ref _result), and then run, and then hold down the ENTER key to run for a few seconds, you can see the difference between the two.

At this point, it reflects the role of interlocked, this article sample source download: Interlocked_sample.

Additional instructions on atomic operations: 32-bit CPU execution assignment instruction, data transmission maximum width of 4 bytes. So as long as the 4-byte read-write operation, the 32-bit CPU is atomic operation. so bool, int these types of operations themselves are atomic operations. The atomic operation method provided by interlocked is performed by the low-level functional CPU instruction encapsulation.

The above is a small set of C # to introduce the use of interlocked for atomic operation of the skills, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.