C # thread,

Source: Internet
Author: User

C # thread,

Processes and threads

A process is a system-level concept used to describe the memory allocation required for a group of resources and programs. Each process has a unique PID. The thread is the basic unit of the process. The first thread created at the process entry point is called the main thread; threads are mainly composed of CPU registers, call stacks, and Thread Local Storage (TLS. The CPU register records the status of the currently executed thread. The call stack is mainly used to maintain the memory and data called by the thread. TLS is mainly used to store the state information of the thread.

Application domain (AppDomain ):.. Net executable programs are carried in the logical partitions of the process. A process can contain multiple application domains, and each application domain carries one.. Net executable program. Each program domain is completely isolated from other program domains in the process (or other processes). Only distributed programming protocols (such as WCF) are used) can access any data in other application domains;

System. AppDomain

A thread can be shuttled across multiple application domains, but at a certain time point, the thread will only be in one application domain.

Context Boundary

Each thread has its own attributes. The Kernel Object of each thread contains a context structure. The context structure exists to be reflected in the last thread execution, the status of the thread CPU register. At any time,WindowsOnly one thread code is allocated to one CPU. One thread allows one time slice to run.After the thread's "time slice" ends, Windows will check all existing thread kernel objects. Only those threads that are not waiting can be used for scheduling. In Windows, select a schedulable thread Kernel Object and switch to it.

Windows has a unique set of standards for selecting a schedulable thread. The rule of the Windows execution thread does not have much to do with the time slice. The thread can be stopped at any time during running, then, in Windows, another thread is scheduled. You have some control over the thread you want to run. However, there is not much control over the thread. It is better not to control it. Remember one thing about thread execution:

You cannot guarantee that your threads are running all the time, and you cannot prevent other threads from running.

Thread priority level 0 ~ 31. In Windows, threads are scheduled in turn in a high-to-low scheduling mode. If a thread with a priority of 31 is finished, Windows will find the next idle thread, if there is a thread with 31 levels in the idle thread, Windows will hand over 31 levels of threads to the CPU for processing.

Process Priority:

Windows supports six process priority types: Idel, Below Normal, Normal, Above Normal, Hight, and Realtime (in turn, Normal is the default process priority, so it is the most commonly used.

Windows supports seven relative thread priorities: Idel, Lowest, Below Normal, Normal, Above Normal, Highest, and Time-Critical.

Relative thread priority

Process Priority

 

Idle

Below Normal

Normal

Above Normal

High

Realtime

Time-critical

15

15

15

15

15

31

Highest

6

8

10

12

15

26

Above Normal

5

7

9

11

14

25

Normal

4

6

8

10

13

24

Below Normal

3

5

7

9

12

23

Lowest

2

4

6

8

11

22

Idle

1

1

1

1

1

16

Remember:If you change the priority class of a process, the relative priority of the thread will not change, but its priority will change..

Windows never schedules a process. It only schedules threads. The "process priority class" is an abstract concept proposed by Microsoft, the purpose is to help you understand the relationship between your application and other running applications. It has no other purpose.

You can change the relative Priority of the Thread. The Priority attribute in the Thread will pass it one of the five values defined in the ThreadPriority Enumeration type, that is, the gray columns in the above table.

ThreadPool

Each process has a thread pool. The default thread pool size is: each available processor has25Threads.You can use the SetMaxThreads method to change the number of threads in the thread pool.

The ThreadPool type has a static method of QueueUserWorkItem. This static method receives a delegate (WaitCallback), representing a custom asynchronous operation.

// A System. Threading. WaitCallback parameter indicates the method to be executed.

// True if this method is successfully queued; System. NotSupportedException is thrown if this work item is not queued.

// System. NotSupportedException: the host that carries the Common Language Runtime does not support this operation.

[SecuritySafeCritical]

Public static bool QueueUserWorkItem (WaitCallback callBack); // enter the method into the queue for execution and specify the object containing the data used by the method. This method is executed when a thread pool thread becomes available.

// System. Threading. WaitCallback, which indicates the method to be executed.

// State: the object that contains the data used by the method.

// True if this method is successfully queued; System. NotSupportedException is thrown if this work item is not queued.

[SecuritySafeCritical]

Public static bool QueueUserWorkItem (WaitCallback callBack, object state );

Benefits of using a thread pool:

Reduces the number of times threads are created, started, and stopped, and improves the efficiency.

This enables us to focus on business logic rather than multi-threaded architecture.

The following scenarios are not suitable for thread pools, but for manual thread management:

  1. When foreground threads are required. Because the threads in the thread pool are always background threads.
  2. The thread must have a specific priority. Because the threads placed in the thread pool are all default priorities (ThreadPriority. Normal), the priority cannot be set.
  3. A task that requires a long time to run. Because the thread pool has the maximum number of threads, a large number of blocked thread pool threads may prevent the task from starting.
  4. If you need a thread with a fixed identifier, it can be easily exited, suspended, or discovered by name.
  5. For COM objects, all threads in the pool are multithreaded apartment MTA threads. Many COM Objects Require single-threaded apartment STA threads;

 

Related Article

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.