Multi-process and multi-threaded selection

Source: Internet
Author: User

1respective features

With regard to threads and processes, the most classic word we had when we were in college was, "the process is the smallest unit of resource allocation and the thread is Minimum unit of CPU Dispatch. Of course, this sentence is enough to cope with the exam, but in the work, the light know that this sentence is useless.

We are in the process of programming, will be tangled with multi-threaded or multiple processes, I can tell you, this problem has no standard answer, reasonable is correct. Depending on the actual project needs, which one is the right choice?


Compare the differences between multi-process and multi-threading below

Compare items

Multi-process

Multithreading

Conclusion

Data sharing

data sharing is complex and needs to be used IPC, simple synchronization

The data is shared, and the synchronization is complex

Each has an advantage

memory,CPU

More memory, more resource-intensive switching

Low memory consumption, easy switching

Thread-dominated

Create, destroy, and switch

Create, destroy, and switch resource-intensive

Less resource-intensive creation, destruction, and switching

Thread-dominated

Programming, commissioning

Simple programming and easy commissioning

Complex programming, complex debugging

Process-dominated

Reliability

Processes are independent of each other and do not affect

A thread hangs up causing the entire process to exit

Process-dominated

Distributed

Suitable for multi-core, multi-machine distributed; if one machine is not enough, it is easier to extend to multiple machines

Suitable for multi-core distributed

Process-dominated


From the above table comparison results, multi-process and multithreading is the relationship between the fish and bear paw.

2How to choose

Through the above comparison, presumably is the choice of multi-process or multi-threading, we have some bottom of the heart.


    • Priority usage threads that require frequent creation and destruction

this kind of application to the Web server, the new connection to create a thread, when the process is finished releasing the thread, if it is a process, the consumption is relatively large. Of course, Apache uses multi-process, this is not a multi-process, but priority to use multithreading.


    • Requires a lot of computation, takes precedence over threads

a lot of calculations will definitely take up a lot of CPU time, when using multithreading to reduce the cost of switching is more appropriate.


    • Strongly related processing with threads, weakly related processing with processes

Strong correlation, it can be understood that the business Contacts close processing, then the weak correlation is the business contact is not close to deal with.

Weak correlation, such as messaging and message processing, Business Contacts are not close, can use multi-process, strong correlation, such as message processing contains message decoding and message processing, the use of multithreading is better.

Of course, this is not a constant way, can also be adjusted according to the actual situation.


    • may use multi-machine distributed, priority to use process

    • Two ways to meet the needs, priority to use their most familiar way

Use your best practices when using multiple processes and multithreading to meet your needs. Of course, the actual development, multi-process and multi-threaded way is coexisting, the two ways are not antagonistic. Suitable and reasonable, reasonable is correct.

3process pool or thread pool

The process pool or thread pool is used to reduce the resources that the program consumes when it is created or destroyed at run time, which has a good effect in times of low computer configuration and can improve the efficiency of the program.


However, it is much more complex to create sub-processes or sub-threads when pre-generating turndown or sub-lines, not only to manage the number of processes/threads in the pool, but also to solve multi-process / Multi-threaded resource-grabbing problems, and, under the current configuration of the computer, The process pool or thread pool is not as efficient as it is to create processes or threads.


Therefore, the use of process pool or thread pool technology, not only complex, from this point of view also has no advantage, in the new application, can be assured that the bold time to create a process or thread.

Multi-process and multi-threaded selection (RPM)

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.