multithreaded modifier volatile in C #

Source: Internet
Author: User
Tags modifier volatile



Volatile is used in C # to control the synchronization of the keyword, its meaning is for some sensitive data in the program, do not allow multithreading simultaneous access to ensure that the data at any access time, a maximum of one thread access to ensure the integrity of the data, volatile is the modifier variable modifiers.


1, volatile use of the scene

Multiple threads access a variable at the same time, and the CLR allows each thread to cache locally for efficiency, which results in a variable inconsistency. Volatile is to solve this problem, volatile modified variables, do not allow the thread to the local cache, each thread of reading and writing are directly operating on the shared memory, which ensures that the variable is always consistent.



2, the volatile keyword can be applied to the following types of fields

(1), reference type

(2), integral type, such as sbyte, Byte, short, ushort, int, uint, char, float, and bool.

(3), an enumerated type with an integer base type.

(4), a generic type parameter that is known to be a reference type.

(5), the local variable cannot be declared as volatile.



I'm afraid it's easiest to explain the difference between volatile and synchronized.              Volatile is a variable modifier, while synchronized acts on a piece of code or method; Look at the following three-sentence get code: int i1;  int Geti1 () {return i1;} volatile int i2;              int Geti2 () {return i2;} int i3; synchronized int Geti3 () {return i3;}

Geti1 () Gets the numeric value stored in the current thread i1. Multiple threads have multiple i1 variable copies, and these i1 can be different from each other. In other words, another thread might have changed the I1 value within its thread, and that value could be different from the I1 value in the current thread. In fact, Java has a mind called the "main" memory area, where the variable is currently "accurate value." Each thread can have its own variable copy, and the variable copy value can be stored differently from the "main" memory area. So there is actually a possibility that the I1 value in the "main" memory area is 1, the I1 value in thread 1 is 2, and the I1 value in thread 2 is 3--this thread 1 and threads 2 All change their respective i1 values, and this change does not occur when passed to the "primary" memory area or other threads.
Geti2 () Gets the i2 value of the "main" memory area. A variable modified with volatile does not allow a copy of a variable different from the main memory area. In other words, a variable that has been modified by volatile must be synchronized across all threads; its value is changed in any thread, and all other threads get the same value immediately. Of course, the volatile-modified variable accesses more resources than a generic variable, because it is more efficient for a thread to have its own variable copy.
Since the volatile keyword has implemented the data synchronization between threads, but also synchronized what to do. Oh, there are two differences between them. First, synchronized gets and releases the monitor--a well-known fact, if two threads use the same object lock, and the monitor enforces that the code block is executed by only one thread at a time. However, synchronized also synchronizes memory: in fact, synchronized synchronizes the entire thread's memory in the main memory area. Therefore, the following steps are performed by the Geti3 () method:
1. Thread requests to get the object lock that monitors this object (assuming that it is not locked, otherwise the thread waits until the lock is released)
2. Thread memory data is eliminated, read from the "main" memory area (Java virtual function optimizes this step ...). [Behind the Don't know how to express, Khan])
3. Code blocks are executed
4. Any changes to the variable can now be safely written to the main memory area (although the Geti3 () method does not change the value of the variable)
5. Thread release object lock for monitoring this object
So volatile only synchronizes the value of a variable between thread memory and "main" memory, while synchronized synchronizes the values of all variables by locking and unlocking a monitor. Obviously synchronized consumes more resources than volatile.


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.