CLR Via C # reading notes----primitive thread synchronization constructs

Source: Internet
Author: User

The focus is on simultaneous access of multiple threads, keeping threads synchronized.

Problems with thread synchronization:

1, thread synchronization is cumbersome, and easy to write wrong.

2, thread synchronization can compromise performance, and it takes time to get and release a lock.

3, thread synchronization allows only one thread to access resources at a time.

class Library and thread safety ,

A thread-safe postback means that the data is not corrupted when two threads attempt to access the data at the same time.

Primitive user-mode and kernel-mode constructs

Primitives: Refers to the simplest constructs in code, with two primitive constructs: User mode and kernel mode.

1, the primitive user mode is faster than the primitive kernel mode because of the direct use of special CPU instructions to coordinate threads that occur in hardware.

2, primitive user mode constructs the disadvantage that only the Windows operating system kernel can stop a thread from running. A running thread in user mode may be preempted, but it will be dispatched at the fastest speed, resulting in a resource being taken, but temporarily leaving the program running in user mode, wasting time.

3, primitive kernel-mode construction: The Windows operating system itself provides, and application threads invoke functions implemented in the system kernel. Switching threads from user mode to kernel mode (or vice versa) can cause significant performance loss. Advantage: The calling thread will block the thread, but it will not waste CPU time.

3, Hybrid construction: Combining primitive user mode and primitive kernel mode advantage

User mode constructs:

Divided into two primitive user-mode thread synchronization constructs: volatile structures and interlocking constructs

Volatile structure: contains atomic or read or write operations on a simple data type

interlocking constructs: Variables that are contained in a simple data type perform atomic read and write operations

CLR Via C # reading notes----primitive thread synchronization constructs

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.