Mission, process, and thread

zhaozj2021-02-08  229

When you are very small, how do you learn how to complete a job, but in adulthood, of course, you have become a parent, at this time, you must learn how to do multiple work at the same time. For example, do you find how many times you have made calls in the office, e-mail and guests? You may be in the office and a person is talking, at this time, the phone is ringing, and the conversation is interrupted by the phone. Maybe when you are calling, you have come again (important) E-mail, you must interrupt your phone. Once you have finished e-mail, you can then call or continue to talk to people in the office. If the time is too long, the person at the other end of the phone may hang up the phone, or the person in the office may be angry. You must decide how to spend your time and who is spent.

In the above example, you are in multi-tasking. You decide how to spend your time and computer to schedule multiple tasks. As we have only 24 hours a day, the resources of the computer are also limited. It seems that you call and handle E-mail at the same time, but in fact, you can only focus on one thing every time you. In fact, you are constantly changing attention, just transform enough fast, just as you do more work at the same time. When the computer is handled multitasking, it is very fast from a program to another program, so that you think that all programs are running at the same time.

Figure 1. From the user's point of view, reading a single task and multitasking

If you have a clone (possibly called Dolly), you can do two things at the same time. For a computer with multiple CPUs, the program is running in each CPU called multiple processing. Sometimes people can replace two concepts with multiple tasks and multiple processing, however, you cannot perform multiple processing on a computer without multiple processors. Therefore, if you are using a computer that has only one processor, the operating system can make multiple tasks, if you are using a computer with multiple processors, the operating system can do multiple task processing and multiple processing .

In the operating system, the process is a logically task. The process is generated when running an application, starting a system service, and starting a subsystem in Windows NT. Each process has its own dedicated resource (such as its own dedicated storage), and only the application of this process can access these resources. This means that if you have a program, you can use some data in the program, and you don't have any form of program data sharing mechanism or use the operating system's program data sharing mechanism, then you can only have your program. Access these data. Most Windows developers simplify the communication between the program with COM objects. If you use Windows NT and want to share data, you can use a memory image file. But this is too technical for Geek Speak Column, so let us continue to talk about it.

Multi-task handle: cooperation and priority

The multi-task processing you can use has two basic methods: cooperation, in this method, the process is running in the process must leave CPU time slice for other processes; priority, in this method, operating system decision Which program gets a time piece. Microsoft Windows 3.x and Macintosh are used by collaborative multitasking, while OS / 2, Windows 95, Windows NT, UNIX, and Amiga operating systems are used by prioritization multitasking.

Collaborative multitasking

If you use a multi-task process of cooperation, each program must allow other programs to use CPUs. There is a special code ring using a cooperative multi-task processing system, which generates control allows the operation of other application software. If everyone works according to the rules, this method will work fairly, but when the application does not accept this rule, he "hevoes" CPU. This means that end users cannot turn to other applications to make the operating system or application "hang". Multi-task handle with priority

In multi-tasking processing with priority, the operating system schedules CPU times, and an application may be suspended (first) at any time. This reduces the problem of "playing", because the operating system is responsible for each application software yourself.

Windows 95 uses priority multitasking for 32-bit Windows applications, in order to be compatible, for 16-bit Windows applications (written for Windows 3.x), WINDOWS applications still adopt multi-tasking processing.

Thread

Thread is a job that is running independently of other parts of the program. Threads belong to a process and get its own CPU time slice. Win32-based applications can use multiple executable threads, called multithreading. Windows 3.x cannot provide a mechanism that naturally supports multi-threaded applications, but some companies written in Windows 3.x use their own thread arrangements.

Win32-based applications can generate multiple threads during a given process. Relying on generating multiple threads, applications can complete some background operations, such as calculations, so programs can run faster. When thread runs, users can continue to affect the program. As mentioned earlier, a corresponding process is generated when an application is running. Then the application can have a separate thread waits for the keyboard to enter or perform an operation, such as offline or calculate the total total number in the spreadsheet.

In the network world, when you try to adjust the performance of your site's server, you have to run the thread. If you are using IIS, you can set the maximum number of threads that can be created for each processor on the server. In this way, you can access your site more evenly accelerate between the processor.

Thread mode

Now, in order to let you know what the thread is and where you can use them, let's take a look at the application that you may want to run when you use the thread: ActiveX component. The ActiveX component is run independent of the COM-based code. Is this not very familiar? When you use ActiveX components, you must register in your operating system. One of these registration information is whether this ActiveX component supports multiple threads, how to support it. This is the thread mode.

The basic thread mode supported by the component is: single thread, unit thread, combined thread. The following sections will talk about what is mean for components.

Single thread

If the component is tagged (ie, registration) is a single threaded component, this means that all executable functions (called method) will run in a shared thread to the component. This is similar to the application that does not generate independent executable threads. The disadvantage of single-threaded components is that only one method can be run at a time. If the component is called multiple times, such as calling the storage method in the component, a bottleneck is generated because there can be only one call at a time.

If you are creating or using an ActiveX component, it is recommended not to use single-threaded components.

Unit thread

If a component is marked as a cell thread, each executable method will run on a thread associated with the component. The reason why the unit thread is because each new component instance has a corresponding thread unit, and each running component has its own thread. The unit thread component is better than a single threaded component because the plurality of components can run simultaneously in their respective cells.

Free thread

A free thread assembly is a multi-threaded component that supports multi-threaded units. This means that multiple method calls can be run simultaneously because each call has its own running thread. This can make your component run more faster, but there are some shortcomings. The unit components running in the same unit can call other components directly in the unit, which is a very fast operation. However, the free thread assembly must call another unit from one unit. In order to achieve this, Win32 generates an agent for use by unit line. This generates system overhead for each required function call, thereby reducing the speed of the system. Each call to free components has a corresponding agent. Since the agent call is slower than the direct call, then naturally decreased performance. Another thing to pay attention to the free thread assembly is: they are not true free. If you have created a free thread component. You still have to make sure that the threads in the component are fully synchronized. This is not an easy task. Just simply mark your component as a free thread, you can't make your components support multithreading, you still have to do your components free thread work. If you don't do this, your shared data may be destroyed. Here, explain why: Let us assume that you have a way to calculate a number and write it to a certain variable. This method is incorporated into an initial value, for example, 4, and the value of this variable is 5 in the subsequent calculation. At the end of the method, this last value is written to the variable. If there is only one calculation process at a time, all this will work very well. However, when the data is being changed, another thread attempts to access it, then the re-get data may be wrong. The following chart illustrates this.

In order to correct this error, developers provide thread synchronization to objects. Thread synchronization is the code that is running when you run a certain code you want to protect. The operating system does not take this code first until a signal that can be interrupted is obtained. If you want to know more about thread synchronization objects, you shouldn't read Geek Speak Column! No, I mean, "Note Take Look at the reference reading document listed later."

Figure 2, shared data is confused by multi-threaded access

Combined thread

Read this, you may think that since each form of thread has its own advantages and disadvantages, why not combine different thread patterns? The combined thread mode may meet your requirements. A component that is labeled as a combined thread existing unit thread components has the characteristics of free thread components. When a component is marked as a combined thread, this component will always be created in the same unit as the unit where it is generated. If the component is created by an object tagged as a single-thread, the behavior of this component will be the same as one unit thread component, and it will be created in the thread unit. This means that the components and the call between the objects are created, and there is no need for a proxy call provided for communication.

If the new component is created by the free thread component, this component will behave like a free thread component, but it will run in the same unit, so the new component can directly access the object that creates it (no proxy call). Remember, if you intend to mark your components as a combined thread, you must provide threads to protect your thread data.

more information

To this end, you should have a basic understanding of the process, job, thread. If you want to know more, these articles may be useful to you. I have to remind you in advance that most of these materials are not prepared for beginners. Good understanding of COM, C , and Win32, will be helpful to understand some articles.

转载请注明原文地址:https://www.9cbs.com/read-1416.html

New Post(0)