On. NET multithreading and parallel Computing (i) preface

Source: Internet
Author: User

As an ASP. NET developer, there is not a lot of exposure to multi-threaded programming in the previous development experience, but as the release of. NET 4.0 approaches, I am increasingly sensing that parallel computing will have a big application in the next 1-2 years. So I decided to write a log to summarize the multi-threaded programming under. NET 3.5 to introduce new parallel libraries provided by. NET 4.0, as well as new parallel programming patterns and programmatic thinking.

I personally feel that in the daily programming for the ASP, the use of multithreaded programming is not a lot, in fact, we enjoy the advantages of multi-threading. First, the Web server environment is a multithreaded environment, each request is a separate thread, if there is no multi-threading it is difficult to imagine the only synchronous processing of a request for a Web server, similar to, our database should also be a multithreaded environment. For programmers of Windows applications, it is difficult to avoid multi-threading, and the simplest thing is that we will take a new thread to do some time-consuming operations, which will prevent the UI from stopping responding and then applying the results of the operation to the controls on the main thread at the end of the operation. Although this application is multi-threaded, and even a lot of programmers are accustomed to what the operation of a new thread to do, but I think this multi-threaded application thinking is still in the single-core era, in the multicore era, we can really let the task actually parallel execution rather than appear parallel execution. Web front end frame best choice!

First of all, the concept, process and thread of the basic concept of not much to say, naturally we can also understand that a process contains at least one thread. By opening multiple threads in a process, we can make a program appear to do multiple things at the same time, for example, to perform some calculations while accepting the user's response. In the previous processor there was often only one core, that is, at the same time, the processor can only do a thing. So how to implement the multiple threads that were previously said to execute at the same time. In fact, at the same time, it just seems to appear at the same time, in essence, multiple threads take up the processor's time slices in turn, and we use their resources in turn, because this time slice is very short, so it seems that a few threads are executed at once for a long time.

to give a vivid example, we often see that some painters can draw two different pictures on a canvas at the same time, one painting a house and the last to finish the painting together. But look carefully to find, he is two hands took two brushes, here draw a pen there draw a pen, at the same time actually also only a pen in painting. The painter should also be single-core like ordinary people, but the thread switch is faster. I often on the phone and chatting with netizens, do two things at the same time, but this is very cost brain, before typing I want to recall the content of the chat, and then enter the chat text, and then to think about what the elder brother said, in the phone back to him, this kind of memory work is to prepare the context of the thread and give it to the brain to deal with. Although two things were done at the same time, the preparation of the context was a waste of time, and if I was talking to a colleague on the phone to do a third thing like watching a movie, I guess I couldn't. Therefore, the thread can not open a lot, especially for the human brain. But for the computer processor is not the same, you just have to prepare the data and instructions he executes is, as to these things from a few things it does not care, 24 hours a second is not wasted in the execution of instructions is absolutely no problem, of course, you can let it idle. Web front end frame best choice!

You might think that since thread switching takes time, then we're running two threads to execute two tasks not yet one execution comes fast? In fact, even for a single-core processor is not necessarily, because in the actual application of our task is often impossible to compute from the beginning to the end of the processor resources, in many cases we have to wait for the IO response or user response, if it is just a thread to do things, the processor is too busy. For today's multicore processors, it is theoretically possible to execute instructions in parallel on each processor at the same time, and we need to use multithreading to improve the speed of operations. Of course not to say that a task to execute 10 seconds, we on the dual-core machine to perform this task in parallel for 5 seconds, it is because many times this task is difficult to divide into two branches to execute in parallel, if each instruction depends on the execution result of the previous instruction, then this operation is difficult to execute in parallel on multiple processors. However, we can think of, at least if there are two of such tasks, we can take full advantage of the advantages of multiple processors to execute in parallel.

But not much can be casually, each thread by default will occupy 1M of the stack space (for ordinary applications), in the 32-bit Windows platform can give a user process to use the largest number of programs in 2G, That is to say that in the program to use the thread can not exceed 2000, in the actual test can be found in general open about 1930 threads will receive an out-of-memory exception, in fact, this quantity is absolutely enough, Even complex Outlook2007 programs typically use only 50 threads (which can be observed in the Task manager).

In the case of forced many people are not very likely to use multithreading is also a reason, because of the complexity of multithreaded programming, we are also accustomed to a line of code execution programming mode, for a good multi-thread program, To split the task as much as possible, let it be used in multiple threads to take advantage of multiple processor cores. Also, if multiple threads are using the same resource, consider locking the resource to avoid inconsistent data. The concept of locking/transactional/concurrency is also very common in databases. The second is because debugging is difficult, especially if the execution of one thread depends on the execution of other threads. Third, because multithreaded programs may not perform as well as the environment (the processor/operating system), if you are programming only for an environment, you may not be able to take advantage of multiprocessor benefits. For example, if we divide a task into 2 threads and execute it in parallel, would it be more reasonable for a quad-core processor to split into 4 threads in parallel? Also, our programming is based on the. NET Framework, and its nature is to use the operating system of the thread, the operating system is already a lot of processes running, the processor is everyone's processor, not exclusively for our program to use, in such a mixed-up environment, our program will not behave as we expected, it is hard to say. Web front end frame best choice!

Multithreading is good, multi-threaded difficult, this series of articles can only be in a relatively superficial level to talk about how in. NET Framework for multithreaded programming, and a typical application of multiple threads in some common applications, such as Windows apps. Hope to be of help to everyone.

On. NET multithreading and parallel Computing (i) preface

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.