Thread----Lightweight synchronization interlocked

Source: Internet
Author: User
Tags semaphore

Fields and properties:

Fields are usually used for methods of classes, and properties are often used to represent the state of a class (such as the Length of a StringBuilder), the ability of a class (such as a StringBuilder capacity), the State or stage of a method

The atomic nature of the object:

The state of an object is a whole, if a field changes. The other fields also need to be changed at the same time. In simple terms, it's either not changed or it's all changed.

The constants of the object:

Once the state of the object is determined, it cannot be changed again. If you want to change again, you need to reconstruct an object

Lightweight sync Interlocked

Why is it lightweight? Because it synchronizes only the shaping data (that is, the int type, the long line). If you have learned the PV operation in the operating system (that is, the semaphore), then you have learned about it generally. It achieves exactly the same function as the semaphore. Here's how it's provided:

Interlocked.Increment (ref value) Value plus one (atomic operation)
Interlocked.decrement (ref value) Value minus one (atomic operation)
Interlocked.exchange (ref value1, value2) Exchange: Assigns the value 2 to the value 1; Returns the new value
Interlocked.compareexchange (ref value1, value2, Value3) To achieve the comparison and exchange of two functions: the value 1 and the value 3 comparison, if the same, the value of 2 to the value of 1, not the same does not do anything; return the original value (more for judging the condition) (used in example 3)

Case (Interlocked.Increment (ref value))

Start 20 tasks, and each task increments the shared data field 10,000 times

Using system;using system.collections.generic;using system.linq;using system.text;using  System.Threading;using System.Threading.Tasks;namespace ConsoleApplication8{     class Program    {        static  Void main (String[] args)         {             state s = new state ();             demo d = new demo (s);             Task[] t = new Task[20];             //Open 20 Quests              for  (int i = 0; i < 20; i + +)             {                 t[i] = new task (D.start);                 t[i]. Start ();            }             //waiting for 20 quests to complete              for  (int i = 0; i < 20; i++)              {                 t[i]. Wait ();            }             console.writeline (s._i);          &nbSp;  console.readkey ();         }    }     //Shared Data Classes     class State    {         public int _i;         Public void increment ()         {            interlocked.increment (ref _i);         }    }    //Quests     class  demo    {        private state state;         public demo (state s)          {            state = s ;      &Nbsp;  }        public void start ()          {             for  (int i = 0; i < 10000; i++)              {                 state. Increment ();            }         }    }}

This article is from the "program Ape's Home--hunter" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1611755

Thread----Lightweight synchronization interlocked

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.