Analysis on building multi-threaded applications in C #

Source: Internet
Author: User

Introduction

1. Understand Multithreading

2. asynchronous thread synchronization

3. Create a multi-threaded Application

3.1 Build through the class of the System. Threading namespace

3.1.1 asynchronous call thread

3.1.2 concurrency Problems

3.1.3 Thread Synchronization

3.2 build multi-threaded applications by Delegation

3.2.1 asynchronous thread

3.2.2 Thread Synchronization

3.3BackgroundWorker component

4. Summary


Introduction

With the promotion of dual-core, quad-core and other multi-core processors, computers with multi-core processors or hyper-threading Single-core processors have become very common. programming technologies based on multi-core processing have also attracted widespread attention from programmers. One of the important aspects is to build multi-threaded applications (because developers cannot fully utilize the powerful performance of multi-core computers without using multithreading ).

This article is intended to build a single-core computer-based multi-threaded application. It aims to introduce the basic concepts and meanings related to multithreading and how to use the System. threading namespace class, delegate, and BackgroundWorker component to build multi-threaded applications.

This article will be satisfied if it can be used to attract others who are new to multithreading. Of course, I am not easy to learn. It is inevitable that there will be deficiencies or errors in this article. Please kindly advise me more.


1. Understand Multithreading

The application we generally understand is *. exe file, when running *. after the exe application, the system will allocate some space for the program in the memory and load some resources required by the program. In fact, this can be called creating a process. You can use the Windows Task Manager to view information about the process, such as the image name, user name, memory usage, and PID (unique process ID,.

The thread is only a basic execution unit in the process. An application usually has only one program entry, such:

 

[STAThread]

Static void Main () // Main entry point of the application

{

Application. EnableVisualStyles ();

Application. SetCompatibleTextRenderingDefault (false );

Application. Run (new MainForm ());

}
A process contains a thread that enters this entry, which is called the main thread. The feature [STAThread] indicates that the default thread model of the application is a single thread unit (for information, see http://msdn.microsoft.com/en-us/library/system.stathreadattribute (VS.71). aspx ). A process that contains only one main thread is thread-safe, which is equivalent to a program with only one working thread. Only after the previous task is completed can the subsequent task be executed.

However, when a program processes a very time-consuming task, such as outputting a large file or remotely accessing the database, the form interface program basically does not respond to the user, menus and buttons are useless. Because the RESPONSE event of the control on the form also needs to be executed by the main thread, while the main thread is busy with other tasks, the control RESPONSE event can only be queued until the main thread is busy and then executed.

To overcome this defect of a single thread, Win32 API allows the main thread to create another thread, but both the main thread and the secondary thread are independent execution units in the process, the shared data can be accessed at the same time, so that the concept of Multithreading is introduced.

I believe that we should have a more emotional understanding of multithreading. However, I would like to remind you that single-core computer-based multithreading is only a blind eye for the operating system (but it will not interfere with our understanding of the idea of building multi-threaded applications ), it cannot shorten the time to complete all tasks, but sometimes it will reduce performance and prolong time by using too many threads. This is because for a single CPU, only one thread can be executed per unit time (also called a time slice), that is, only one task can be done. When the time slice of a thread is used up, the system suspends the thread and executes another thread in the next time, the CPU uses time slice as the interval to execute operations alternately among multiple threads (in fact, this operation is also related to the priority of each thread, and a high level will give priority to processing ). Due to the short switching interval, the illusion that each thread is "simultaneously" working is generated. If the number of threads is too large, the current State data of the thread should be recorded when the system suspends the thread, this will inevitably reduce the overall performance of the program. But for these, multi-core computers can improve program execution efficiency in essence (truly working at the same time.


2. asynchronous thread synchronization

The thread-based task execution methods can be divided into thread synchronization and thread Asynchronization. For ease of understanding, "synchronous thread" is used in the subsequent description to refer to threads related to thread synchronization. Similarly, "asynchronous thread" is used to represent threads related to thread Asynchronization.

Thread Asynchronization solves the problem that interface controls cannot be used when time-consuming tasks are executed. For example, if you create a thread to specifically execute time-consuming tasks, and other tasks such as interface control response are handed over to another thread for execution (usually executed by the main thread ). In this way, the two threads can simulate multiple tasks by switching between threads in a short time (time slice).

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.