C # Programming Summary (ii) Multithreading basics

Source: Internet
Author: User
Tags execution resource thread

Whether you are developing a computer with a single processor or a computer that has multiple processors, you want the application to provide the best response performance for the user, even if the application is currently completing additional work. One of the most powerful ways to enable applications to respond quickly to user actions while leveraging the processor between user events, or even during user events, is to use multithreaded technology.

Multithreading: Threads are a single sequential control process in a program. Running multiple threads at once in a single program to accomplish different tasks, called multithreading. If a thread makes a long delay operation, the processor switches to another thread for execution. This enables concurrent (concurrent) execution of multiple threads to hide long delays, increasing processor resource utilization, and thus improving overall performance. Multithreading is to achieve multiple tasks synchronously, not to improve operational efficiency, but to improve the efficiency of resource utilization to improve the system

A, process and thread

Process, is the operating system for resource scheduling and allocation of the basic unit. is composed of the process Control block, the program section, the data section three parts. A process can contain several threads (thread), which can help the application do several things at the same time (for example, one thread writes to the disk, the other receives the user's keystrokes and responds in a timely manner, and does not interfere with each other), and after the program is run, The first thing the system has to do is create a default thread for the program process, and then the program can add or remove the associated threads as needed. It is a program that can be executed concurrently. The running process on a data set is an independent unit of resource allocation and dispatch, also called activity, path or task, which has two aspects: activity and concurrency. Processes can be divided into running, blocking, ready three states, and changing to one another with certain conditions: ready-run, run-block, block-ready.

Thread, which is the smallest unit of CPU scheduling and execution. Sometimes referred to as lightweight processes (lightweight PROCESS,LWP), is the smallest unit of program execution flow. A standard thread consists of a thread ID, a current instruction pointer (PC), a register collection, and a stack. In addition, a thread is an entity in a process, the basic unit of a system that is independently dispatched and dispatched, and the thread itself does not own system resources and has only a few essential resources in operation, but it can share all the resources owned by the process with other threads of the same process. One thread can create and undo another thread, which can be executed concurrently between multiple threads in the same process. Because of the mutual restriction between threads, the thread appears intermittent in operation. Threads also have three basic states of readiness, blocking, and running.

The main thread, when the process is created, defaults to the creation of a thread, which is the main path. The main thread is one that produces other child threads, while the main thread must be the last to end execution, and it completes various operations that close other child threads. Although the main thread is created automatically at the beginning of a program, it can also be controlled by Thead class objects, by invoking the CurrentThread method to obtain the current thread's reference

Advantages of Multithreading: processes have separate address spaces, and threads within the same process share the address space of the process. Starting a thread takes far less space than it takes to start a process, and the time it takes to switch between threads is much less than the time it takes to switch between processes.

Second, the advantages of multithreading

1, improve application response. This is especially useful for graphical interface programs, when an operation takes a long time, the whole system will wait for this operation, when the program does not respond to keyboard, mouse, menu operation, and using multithreading technology, the time-consuming operation (time consuming) on a new thread, you can avoid this embarrassing situation.

2, make the multi-CPU system more effective. The operating system guarantees that when the number of threads is not greater than the number of CPUs, different threads run on different CPUs.

3, improve the structure of the program. A long and complex process can be considered into multiple threads to become several independent or half independent parts of the operation, such programs will facilitate understanding and modification.

Despite the obvious advantages of multithreading, thread concurrency conflicts, synchronization, and management tracking can bring a lot of uncertainty to the system, which must be given enough attention.

Nonsense not much to say start our multithreaded journey.

Third, the application of Multithreading:

Briefly summed up, there are generally two situations:

1 multiple threads, complete similar tasks, improve concurrency performance

2 A task has several independent steps, multiple threads execute subtasks concurrently, improve task processing efficiency

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.