Why multi-process/thread

Source: Internet
Author: User

 

  

What is a process (Process): The general explanation is that a Process is a program execution, and what is a Thread, a Thread can be understood as a program fragment of execution in a Process. In a multitasking environment, the following concepts help us understand the differences between the two:

  • Processes are independent, which is manifested in the memory space and Context Environment. threads run in the process space.
  • Generally, (no special technology is used) A process cannot break through the process boundary to access the storage space in other processes. Because the thread is in the process space, therefore, threads generated by the same process share the same memory space. (Figure 1)
  • Two segments of code in the same process cannot be executed simultaneously unless the thread is introduced.
  • A thread is a process. When a process exits, all threads generated by the process are forcibly exited and cleared.
  • Threads consume less resources than processes.
  • Both processes and threads can have priority.
  • A process in a thread system is also a thread. The process can be understood as the first thread of a program.

Figure 1

The simplest example is to draw multiple beating balls on the screen. We can use a thread to draw each ball. However, there are few cases where such threads are completely independent without affecting data exchange.

Let's take an example. An application needs to complete two tasks:Write 1000 random numbers into the file, read data from the file, circle the random number as the center, perform this operation 100 times, and save the file using 100 different files. The traditional approach is as follows:

Void do_this (void)
  
{
  
For (int I = 0; I <100; I ++)
  
{
  
/// Step 1
  
Generate 1000 randam number;
  

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.