Multithreading-Understanding Threads

Source: Internet
Author: User

First, what is a thread

A thread is a set of instructions, or a special section of a program that can be executed independently in a program. It can also be understood as the context in which the code runs. So a thread is basically a lightweight process that is responsible for multitasking in a single program. Typically, the operating system is responsible for scheduling and executing multiple threads.

Second, the cost of the thread

Threads generate the consumption of space (memory consumption) and time (execution performance at run time).

(1) Thread kernel object

Contains a set of properties that describe the thread, the thread context.

(2) Thread environment block (thread environment block)

Allocates a block of memory initialized in user mode. It contains the top of the thread exception handling chain. When a thread enters a try block, it inserts a node at the top of the chain and deletes the node when it exits.

(3) User mode stack

Stores the local variables and arguments passed to the method. Also contains the address of the thread where the current method should start when it returns. The user mode stack is assigned 1M by default.

(4) kernel mode stack

Program to the operating system kernel-mode function bed-bottom argument, will use the kernel-mode stack. Any arguments from the user-mode code bed to the kernel will be copied from the thread's user-mode stack to the thread's kernel-mode stack.

(5) DLL thread connection and detach notification

A process creates a thread that loads all the DLLs for the process and passes a connection notification. Conversely, all DLLs are called back, and a separate notification is passed.

(6) Context switch

At any one time, the system assigns only one thread to a CPU and can run a time slice, and the time slice expires, and a context switch occurs, switching to another thread.

A, for a thread that is about to end running, it has a value in the CPU register saved to the context structure of this thread kernel object.

B, select the new thread to toggle the load on, and load the context of this thread into the CPU register.

C, the CPU executes the selected new thread, runs a time slice, as on the switch again ...

Attention:

In this case, if a program infinite loop, the system will be preempted periodically.

If you want to switch to the same thread, the context is not toggled.

To perform garbage collection, the CLR must pause all threads, traverse the stack, find the root, and mark the objects in the team. Restores all threads.

Multithreading-Understanding Threads

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.