CLR via C # Reading Notes 4-1 thread synchronization-common locks, native user mode and core mode (on)

Source: Internet
Author: User

This article mainly describes several different solutions to ensure thread synchronization and data security.

Take the lock as an example:

1. User-mode is much faster than kernel-mode.

2. the Windows operating system cannot find the user mode lock (even if it has been blocked), so the thread pool will not create a new thread to process new requests.

3. The lock in the core mode can stop the thread running, so that the CPU is not wasted, but the memory is wasted (when the conditions are met, Windows will wake up the previously dormant thread to continue execution)

4. User Mode Locking. In fact, the thread is still running, wasting CPU and memory.

5. The lock in the core mode will greatly damage the performance.

PS: the permanent locking of the user mode is called livelock, and the permanent locking of the core mode is called deadlock.

Considering the above situations

The user mode should be given priority when the lock time is short.

The core mode should be given priority when the lock takes a long time, because it will not waste CPU while waiting (I personally think that all those waiting for more than 1 ms should use the core Mode)

 

FCL has two native user mode locks: (we strongly recommend that you use multiple threads.ProgramApplying interlocked)

1. Volatile: marking a simple data type as volatile will make the read/write operations of this variable atomic (do not mark all the variables as volatile for convenience)

2. interlocked. This class encapsulates some operations for simple data types, such as exchange, compareexchange, and add (this class is very useful)

PS: I don't understand why interlocked does not support simple data types such as Boolean ......

 

 

 

 

Noun Translation:

User-Mode)

Kernel-mode)

 

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.