Excerpt from-it Enterprise must read 200. NET face questions-07. NET multithreaded Programming

Source: Internet
Author: User

The basic concept of multithreading

Q: Please explain the threads and processes at the operating system level

A process represents an entity for a running application, and a process can contain 1 or more threads.

Q: are multithreaded threads executed in parallel in the operating system?

On a single-CPU computer architecture, there can be only one running thread at any time, and the operating system makes the user feel that multithreading is executing at the same time through fast scheduling rotations. On a multi-CPU architecture, there may be a fully parallel thread.

Q: What is fiber

Fiber is the concept of lightweight threading proposed by Microsoft, a fiber that has its own stack and register status. A thread can contain more than one fiber, and the thread is different from the operating system scheduling, the thread's fiber schedule is completely controlled by the programmer, and the kernel of the operating system does not know the existence of the fiber at all.

. Multi-threaded programming in net

Q: How to use. NET's program pool

The System.Threading.ThreadPoll type encapsulates the operation of the program pool. Each process has a thread pool. NET provides a mechanism for program pool management, all threads in the program pool are background threads, and they do not prevent the program from exiting.

Q: How to define a thread-exclusive global data

Thread-local storage is a mechanism that provides a thread-exclusive global Data feature. In. NET, you can use SetData and GetData of the thread type to manipulate TLS data. At the same time,. NET provides the data called the ThreadStatic attribute to assert that the thread is exclusive.

Q: How to prevent thread execution context delivery

The execution context of a thread is a wrapper for all thread contexts, and in general, the execution context of the current thread is automatically streamed into the newly created thread. You can use the Unsafequeueuserworkitem method defined in the System.Threading.ThreadPool type and the Suppressflow method defined in the ExecutionContext type to prevent such a flow.

Thread synchronization for multi-threaded threads

Q: What are synchronization blocks and synchronization block indexes

Synchronization blocks are referred to as. NET maintains an element in the synchronization block array that provides the action of thread synchronization, and when a thread owns a synchronization block, other threads enter a wait state when they attempt to access the synchronization block. A synchronized block index is a field that is assigned to objects within each heap.

What does the q:c# lock keyword do?

The lock keyword is essentially a simplified syntax for the Monitor.Enter and monitor.exit two methods that enable synchronization of an object to enter and exit.

Q: Can I synchronize the reference type object itself

Synchronizing the reference type itself refers to code similar to lock (this). The use of the object itself as a synchronization object can result in a lack of robustness for the type. When a type consumer maliciously consumes an object's synchronization block for a long time, all other users will be deadlocked.

Q: What is the difference between the mutex type and the function of the monitor type?

The mutex is the kernel object of the synchronization thread within the operating system and has the corresponding WIN32 function to manipulate the mutex object. In. NET, the mutex type encapsulates the operation of all mutexes, and the role of the mutex type can span the process, as opposed to the monitor type, because it is done in the kernel of the operating system, so the performance of the mutex type is relatively poor.

Excerpt from-it Enterprise must read 200. NET face questions-07. NET multithreaded Programming

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.