This comprehensive study of multithreading related knowledge, first from the basic concepts, and then step by step in-depth and learn the source code.

The following content is mainly from Baidu Encyclopedia entry.

process

A process is an instance of a running program. When a program enters memory and runs, it becomes a process. In iOS, the start of an App is to start a process.

A Process is a running activity of a program in a computer on a certain data set. It is the basic unit of the system for resource allocation and scheduling. Each Process is independent and runs in its dedicated and protected memory, which is the basis of the operating system structure.

In the early process-oriented computer architecture, the process is the basic execution entity of the program. In modern thread-oriented computer architectures, processes are containers for threads. A program is a description of instructions, data and their organizational form, while a process is an entity of the program.

A process is a running activity of a program with independent functions about a set of data. It can claim and own system resources, is a dynamic concept, is a moving entity. It is not just the code of the program, but also the current activity, represented by the value of the program counter and the contents of the processing registers.

define

  • Narrow definition: A process is an instance of a running program. When a program runs in memory, it becomes a process. iniOSaAppTo start is to start a process.
  • Broad definition: a process is a program with some independent function about a data set of a running activity. It is the basic unit of dynamic execution of operating system. In traditional operating system, process is both the basic unit of allocation and the basic unit of execution.

The concept of a process has two main points: first, a process is an entity. Each process has its own address space, which generally includes text regions, data regions, and stack regions. The text area stores the code executed by the processor; The data area stores variables and dynamically allocated memory used during process execution; The stack area stores the instructions and local variables of the active procedure call. Second, a process is an “ongoing program.” A program is an inanimate entity, and only when it is given life by the processor (executed by the operating system) can it become a living entity, we call it a process.

Process is the most basic and important concept in operating system. It is a concept introduced after the appearance of multi-channel programming system in order to describe the dynamic situation inside the system and describe the activity law of each channel program inside the system. All multi-channel programming operating systems are built on the basis of process.

Multi – program system is to store several independent programs in the computer memory at the same time, so that they are under the control of the management program, interspersed with each other. The state of two or more programs in a computer system between the beginning and the end. This is called the characteristic of multichannel technology operation: multichannel, macro concurrent, micro serial.

The reason why the process was introduced

The operating system introduced the concept of processes for:

  • From a theoretical point of view, it is an abstraction of a running program process.
  • From the realization point of view, is a data structure, the purpose is to clearly depict the internal law of dynamic system, effective management and scheduling into the computer system main memory running program.

Process characteristics

  • Dynamic: the essence of a process is a process of execution in a multi-program system. The process is dynamically generated and dies out.
  • Concurrency: Any process can execute concurrently with other processes.
  • Independence: A process is a basic unit that can operate independently, as well as an independent unit of system resource allocation and scheduling.
  • Asynchronism: The execution of processes is intermittent due to their mutual constraints, that is, processes move forward at independent and unpredictable speeds.
  • Structure features: process is composed of program, data and process control block.

Multiple different processes can contain the same program: a program in different data sets constitutes a different process, can get different results; But during execution, the program cannot be changed.

Process control block

Processing Control Block is a data structure in the core of the operating system, which mainly represents the process status. Its function is to make a program (including data) that can not run independently in the multi-program environment become a basic unit that can run independently or execute concurrently with other processes. In other words, OS controls and manages concurrent processes according to PCB. PCB is usually a system memory footprint area of a continuous storage area, storage of the operating system is used to describe the process condition and all the information necessary to control the operation of process, it makes a in a multiprogramming environment can’t be a stand-alone program can run independently of the basic unit or a process that can execute concurrently with other processes.

The basic content

A process control block (PCB) is a specialized data structure set up by the system to manage processes. The system uses it to record the external characteristics of the process, describe the process of motion change process. At the same time, the system can use PCB to control and manage the process, so PCB (process control block) is the only sign that the system senses the existence of the process.

Record information

PCB usually records process-related information, including:

  • Program counter: Address of the instruction to be run next.
  • The process state can be New, Ready, RUNNING, Waiting, or Blocked.
  • CPU memory: such as accumulator, Index register, stack pointer and general purpose memory, status code, the main purpose is to interrupt the temporary storage of data, in order to continue to use later; The number and category vary due to computer architecture.
  • CPU scheduling: pointer to priority, queue, and other parameters.
  • Memory management: such as TAB page tables, etc.
  • Accounting information: such as CPU and actual time usage number, duration, account number, job or process number.
  • I/O status: The configuration process uses I/O devices, such as tape drives.

organization

Process control block PCB organization

  1. Linear table mode: regardless of the state of the process, all PCB continuously stored in the system area of memory. This approach is suitable for a small number of processes in the system.
  2. Index table mode: this mode is an improvement of linear table mode. The system establishes ready index table and blocked index table respectively according to the status of the process.
  3. Link table mode: The system queues the PCB of the process according to the state of the process, thus forming ready queue, blocking queue, running queue and so on.

content

A computer system process contains (or “owns”) the following data: a memory image of the program’s runnable machine code. Storage allocated (usually including an area of virtual memory). The contents of the storage include runnable code, process-specific data (input, output), call stacks, and stacks (used to hold data generated midway through run-time runs). Operating system descriptors for resources assigned to the process, such as file descriptors (Unix terms) or file handles (Windows), data sources, and data terminals. Security features, such as the process owner and the permission set (permissible operations) of the process. Processor state (text), such as register contents, physical memory addressing, etc. The state is usually stored in registers while the process is running, and in other cases in memory.

switch

To do a process switch is to retrieve the processor from the running process and then have the running process occupy the processor.

To reclaim a processor from a process is essentially to store intermediate data stored in the processor’s registers in a place where the processor’s registers can be freed up for other processes to use. Where is the intermediate data stored for the aborted process? Of course, this should be the process’s private stack.

Process to take up the processor, in essence is to put a process in private stack register data when the process is suspended (the last) in the middle of the data into the processor’s registers again, and the process to run into the breakpoint processor program pointer PC register (PC), then began to run process are processors to run, That is, the process already owns the processor.

This is like a number of students to use the same desk, the so-called to recover the desk is using the right to use the desk, in essence, let him take away what belongs to him; To give a student the right to use his desk is simply to ask him to put his things on it.

In the process of switching, the intermediate data stored in the registers of the processor is called the context of the process, so the process switching is essentially the context switching between the suspended process and the process to be run. When the process is not occupying the processor, the context of the process is stored in the process’s private stack.

state

The discontinuity of the process execution determines that the process may have multiple states. In fact, a running process can have three basic states.

  1. Ready state (Ready) :

The process has obtained the required resources except the processor and is waiting to allocate the processor resources. It can be executed as long as the processor process is allocated. A ready process can queue by multiple priorities. For example, when a process enters the ready state due to the time slice running out, it is put into the low-priority queue. When a process enters the ready state after an I/O operation, it is placed in a high-priority queue.

  1. Operating state (Running) :

Processes occupy processor resources; The number of processes in this state is less than or equal to the number of processors. Idle processes of the system are usually automatically executed when no other processes are available (for example, when all processes are blocked).

  1. Blocking state (Blocked) :

Because a process is waiting for some condition (such as an I/O operation or process synchronization), execution cannot continue until the condition is met. This event cannot run even if processor resources are allocated to the process before it occurs.

The difference between

Processor: PROCESSOR includes the central processing unit (CPU), main memory, input-output interfaces, and peripherals to form the complete computer system. A processor is a unit that processes stored programs and data in a computer system and executes instructions according to the steps specified in the program. A program is a sequence of instructions that describes how a processor performs a task. Instruction is a unit of information which is interpreted and executed directly by the processing function.

Program: Program is an ordered collection of instructions and data, which itself has no meaning of operation, is a static concept. Process is a dynamic concept, which is the execution process of a program on a processor. A program can exist as a software document for a long time, while a process has a certain life cycle. Programs are permanent, processes are temporary. Processes describe concurrency more realistically than programs do. The process is composed of three parts: process control block, program segment and data segment. Processes have the ability to create other processes, but programs do not. The same program runs on several data sets at the same time, it will belong to several different processes, that is to say, the same program can correspond to more than one process. In traditional operating systems, programs can not run independently, as the basic unit of resource allocation and independent operation are processes.

Thread: Usually can contain a number of threads in a process, they can take advantage of the process has the resources, the introduction of the thread of the operating system, usually the process as the basic unit of the allocation of resources, the thread as the basic unit of the independent running and scheduling, because the thread process than smaller, basically does not have the system resources, Therefore, the cost of its scheduling will be much smaller, which can improve the degree of concurrent execution of multiple programs in the system more efficiently. Today’s general-purpose operating systems have introduced threads in order to further improve system concurrency, and it is regarded as an important indicator of modern operating systems.

control

Process control is the most basic function in process management. It is used to create a new process, to terminate a completed process, or to terminate a process that is unable to run due to an event, and to take care of state transitions during process execution.

Create a process
  1. The event that causes the process to be created

In a multiprogram environment, only processes can run in the system. Therefore, in order for a program to run, a process must be created for it. Typical events that cause one process to create another can fall into the following four categories:

  • The user login

In a time-sharing system, after a user enters a login command at a terminal, if it is a legitimate user, the system will set up a process for the terminal and insert it into the ready queue.

  • Job scheduling

In a batch system, when the job scheduler schedules a job according to a certain algorithm, it loads the job into memory, allocates necessary resources for it, and immediately creates a process for it, and then inserts it into the ready queue.

  • To provide services

When running in the user program put forward a request, the system will create a special process to provide the service according to user’s requirements, for example, a user program requires file type, operating system will create a printing process, for it in this way, not only can make the printing process and the user process concurrent execution, but also easy to calculate for the time it takes to print task.

  • The application request

In all three cases, the system kernel creates a new process for it, while this type of event creates a new process based on the requirements of the application process, so that the new process can run concurrently to complete a specific task.

  1. Process creation process

Primitives – Control of computer processes is usually done by primitives. The so-called primitive, generally refers to a program segment composed of several instructions, used to achieve a specific function, in the execution process can not be interrupted. In the operating system, certain operations called by a process, such as queue operations, operations on semaphores, checking startup peripherals, etc., cannot be interrupted once the execution starts, otherwise there will be operation errors, causing chaos in the system. As a result, these operations are implemented using primitives, which are part of the operating system core (consisting not of processes but of a set of program modules), resident in memory, and usually executed in a tube. Once a primitive is executed, it is executed continuously without interruption.

Once the operating system detects an event that requires the creation of a new process, it calls the process creation primitive create() to create a new process by following these steps.

  • Application blankPCB. Request a unique numeric identifier for the new process and obtain it fromPCBAsk for a blank space in the collectionPCB.
  • Allocate resources to the new process. Allocate the necessary memory space for the new process’s programs and data as well as the user stack. Obviously, at this point, the operating system must know how much memory the new process needs.
  • Initialize the process control block.PCBInitializations include:

(1): initialize the identification information, and fill the identifiers assigned by the system and the parent process into the new PCB. (2): initialize the processor state information to make the program counter (PC) point to the entry address of the program and the stack pointer point to the top of the stack. (3): initialize processor control information, set the state of the process to ready state or static ready state, for priority, usually set it to the lowest priority, unless the user explicitly requests high priority.

  • Inserts a new process into the ready queue, or if the ready queue can accept the new process.
Process terminates
  1. An event that causes a process to terminate
  • The end of the normal

In any computer system, there should be an indication that the process has finished running. In batch systems, for example, a Hold instruction or terminating system call is usually scheduled at the end of the program. When a program runs to the Hold command, an interrupt is generated to inform the OS that the process has completed.

  • Abnormal end

During the running of a process, certain errors or failures force the process to terminate. There are many such abnormal events, including: out of bounds error, protection error, illegal instruction, privileged instruction error, run timeout, wait timeout, arithmetic operation error, and I/0 fault.

  • Outside intervention

External intervention does not mean that an abnormal event occurs during the running of the process, but that the process terminates at the request of the outside world. These interventions are: operator or operating system intervention, parent process request, parent process termination.

  1. The termination of a process

If a process termination event occurs, the OS invokes the process termination primitive and terminates the specified process as follows.

  • From the terminated process identifierPCBCollection to retrieve the processPCBTo read the process state from.
  • If the terminated process is in the execution state, the execution of the process should be terminated immediately, and the juxtaposed scheduling flag is true. Used to indicate that the process should be rescheduled if terminated.
  • If the process has descendant processes, all descendant processes should also be terminated to prevent them from becoming uncontrollable processes.
  • All resources owned by the terminated process are returned either to its parent process or to the system.
  • The process will be terminated (itsPCB) moves out of the queue (or linked list) and waits for other programs to collect information.
Blocking the wake up
  1. An event that causes a process to block and wake up
  • Requesting system services

When a running process requests the operating system to provide services, but the operating system does not immediately meet the requirements of the process for some reason, the process can only change to the blocking state and wait. Once the requirements are satisfied, the process is woken up.

  • To initiate an operation

After a process starts an operation, if the process cannot continue until the operation is complete, the process must be blocked until the operation is complete. After the operation is complete, the process is woken up.

  • The new data have not arrived yet

For cooperative processes, if one of the processes needs data from the other (cooperative) process before it can run to process the data, the process must block (wait) until the data arrives and wake up.

  • There are no new jobs to be done

The system often sets up some system processes with certain functions. When this process completes its task, it blocks itself to wait for the arrival of a new task. When the new task arrives, the process is woken up.

  1. Process blocking procedure

When the executing process detects one of the above events, it blocks itself by calling the blocking primitive block() because it cannot continue executing. It can be seen that process blocking is an active behavior of the process itself. After entering the block process, since the process is still in execution state at this moment, it should immediately stop execution, change the current state in the process control block from execution to blocking, and insert the PCB into the blocking queue. If the system has multiple blocking queues that block for different events, this process should be inserted into a blocking (wait) queue with the same event. Finally, the scheduler reschedule, assign the processor to another ready process, and perform a switch, that is, preserve the processor state of the blocked process (in the PCB), and then set the CPU environment according to the processor state of the new process’s PCB.

  1. The process by which a process is awakened

When the event expected by the blocked process appears, such as I/O completion or the expected data has arrived, the relevant process (for example, the process that has exhausted and released the I/O device) invokes weakUp () to wake up the process waiting for the event. The wake primitive performs the process of first removing the blocked process from the blocking queue waiting for the event, changing its current state in the PCB from blocked to ready, and then inserting the PCB into the ready queue.

Scheduling algorithm

The process scheduling algorithm includes: FIFO(First Input First Output), SJF(Shortest Job First), SRTF(Shortest Remaining Time First). Interactive system: RR(Round Robin), HPF(Highest Priority First), multi-level queue, shortest process First, guaranteed scheduling, lottery scheduling, fair sharing scheduling.

phase

The process is composed of three parts: process control block, program segment and data segment. A process can contain several threads, which help the application to do several things simultaneously (for example, one Thread writes files to disk, and the other receives user keystrokes and responds to them without interference). After the program is run, The first thing the system needs to do is to create a default thread for the application process (in iOS, the App starts a main thread for us by default), and then the application can add or delete related threads as needed. Is a program that can be executed concurrently. The running process on a data set is an independent unit of the system for resource allocation and scheduling. It is also called activity, path or task. It has two properties: activity and concurrency. A process can be divided into three states: running, blocked, and ready, which are interchangeable with certain conditions: ready – run, run-blocked, and blocked – ready.

A process is a running instance of an application and a dynamic execution of the application. Seemingly profound, we can simply understand as: it is the operating system is currently running the executive program. In the system currently running the execution program includes: system management computer individual and complete a variety of operations necessary procedures; Additional programs that the user starts or executes, including, of course, illegal programs that run automatically without the user’s knowledge (they may be viruses).

thread

A thread is the smallest unit in which an operating system can schedule operations. It is contained within the process and is the actual operating unit within the process. A thread is a single sequential flow of control in a process, and multiple threads can be concurrent in a process, each performing a different task in parallel. In Unix System V and SunOS, they are also referred to as Lightweight processes, but more commonly referred to as kernel threads, while user threads are referred to as threads.

Threads are the basic unit of independent scheduling and dispatch. Threads Kernel threads that can be scheduled for the operating system kernel, such as Win32 threads; User threads scheduled by user processes, such as POSIX threads on Linux. Or it can be mixed by the kernel and user processes, such as Windows 7 threads.

Multiple threads in the same process share all system resources in that process, such as virtual address space, file descriptors, signal processing, and so on. But multiple threads in the same process have their own call stack, their own register context, and their own thread-local storage.

A process can have many threads, each performing different tasks in parallel.

The benefits of using multithreaded programming on a multi-core or multi-CPU, or hyper-threading enabled CPU are obvious, namely increased program throughput. In a single CPU single-core computer, the use of multithreading technology, can also be responsible for THE PROCESS of I/O processing, human-computer interaction and often blocked part and the part of intensive computing to separate the execution, write a special workhorse thread to perform intensive computing, thus improving the efficiency of the program.

The development history of

The introduction of thread: in the 1960s, the basic unit that can have resources and run independently in OS is process. However, with the development of computer technology, process has many disadvantages. First, because process is the resource owner, creating, revoking and switching have large time and space overhead, so it needs to introduce light process; Second, due to the emergence of the peer process multi-processor (SMP), it can meet the requirements of multiple operating units, and the parallel cost of multiple processes is too large. Thus, in the 1980s, came the basic unit that could operate independently: threads.

Scope of application

  1. File management or communication control in the server.
  2. Front and back processing.
  3. Asynchronous processing.

The characteristics of

In a multithreaded OS, there are usually multiple threads in a process, and each thread is the basic unit of CPU utilization and the entity with the minimum overhead. Threads have the following properties.

  1. Light entity

The entities in the thread basically have no system resources, just a few essential resources that can be guaranteed to run independently. Thread entities include programs, data, and TCBS. Thread is a dynamic concept whose dynamic characteristics are described by Thread Control Block (TCB). TCB includes the following information:

  • Thread status.
  • An on-site resource that is saved when the thread is not running.
  • A set of execution stacks.
  • Holds the local variable main memory area for each thread.
  • Access main memory and other resources in the same process.

A set of registers and stacks used to indicate program counters, reserved local variables, a few state parameters, return addresses, and so on for the sequence of instructions to be executed. 2. Basic unit of independent dispatch and dispatch. In a multithreaded OS, the thread is the basic unit that can run independently, and thus the basic unit of independent scheduling and dispatching. Because threads are “light,” thread switching is very fast and inexpensive (in the same process). 3. Concurrent execution can be performed. Multiple threads in a process can execute concurrently, or even all threads in a process can execute concurrently. Similarly, threads in different processes can execute concurrently, taking full advantage of the processor’s ability to work in parallel with peripherals. 4. Share process resources. Each thread in the same process can share the resources owned by the process. First, all threads have the same address space (the address space of the process), which means that the thread can access every virtual address in the address space. In addition, you can access open files, timers, semaphore mechanisms, and so on owned by the process. Because threads within the same process share memory and files, threads communicate with each other without calling the kernel.

Comparison with process

Processes are the basic unit of resource allocation. All resources associated with the process are recorded in the process control block PCB. To indicate that the process owns these resources or is using them. In addition, the process is also the scheduling unit of the preemption processor, and it has a complete virtual address space. When a process is scheduled, different processes have different virtual address Spaces, and different threads within the same process share the same address space. Threads, in contrast, are independent of resource allocation; they belong to a process and share its resources with other threads within the process. Threads consist only of the relevant stack (system stack or user stack) registers and the thread control table TCB. Registers can be used to store local variables in a thread, but not related variables in other threads. Typically, there can be several threads in a process that can take advantage of the resources owned by the process. In operating systems that introduce threads, it is common to regard processes as the basic unit of resource allocation, and threads as the basic unit of independent operation and independent scheduling. Because the thread is smaller than the process, basically does not have the system resources, so the cost of its scheduling will be much smaller, can more efficiently improve the degree of concurrent execution between multiple programs in the system, so as to significantly improve the utilization rate of system resources and throughput. Therefore, threads have been introduced into the general operating system in recent years in order to further improve the concurrency of the system, and it is regarded as an important indicator of modern operating systems. The differences between threads and processes can be summarized in the following four points:

  1. Address space and other resources (such as open files) : processes are independent of each other and are shared by threads of the same process. Threads in one process are not visible to other processes.
  2. Communication: Inter-process communication IPC, threads can directly read and write process data segments (such as global variables) to communicate — need the assistance of process synchronization and mutual exclusion means to ensure data consistency.
  3. Scheduling and switching: Thread context switching is much faster than process context switching.
  4. In a multithreaded OS, a process is not an executable entity.

Synchronization of threads

The synchronization of threads is a difficulty in multithreaded programming. Developers often do not know what competing resources are, when to consider synchronization, how to synchronize and so on. Of course, there is no clear answer to these questions, but some principles need to be considered, whether competing resources are changed at the same time?

For synchronization, you need to do two things in the concrete Java code: mark competing resources as private; Synchronize code that modifies variables, using the synchronized keyword to synchronize methods or code. Of course, this is not the only way to control concurrency security. Usage description synchronized can only mark non-abstract methods and cannot identify member variables.

The working principle of

A thread is an entity in a process. A process can have multiple threads, and a thread must have a parent process. Threads do not own system resources, only the necessary data structures to run; It shares all resources owned by the parent process with other threads. Threads can create and undo threads, enabling concurrent execution of programs. In general, threads have three basic states: ready, blocked, and running.

In a multi-CPU system, different threads can run on different cpus at the same time, even when they belong to the same process. Most operating systems that support multiple processors provide programming interfaces that allow processes to control their threads’ affinity with each processor.

Threads are sometimes called lightweight processes. Like a process, threads are independent, concurrent paths of execution within a program, with each thread having its own stack, its own program counters, and its own local variables. However, there is less isolation between threads in a process than in a separated process. They share memory, file handles, and other state that each process should have.

A process can support multiple threads that appear to be executing simultaneously, but are not synchronized with each other. Multiple threads in a process share the same memory address space, which means they can access the same variables and objects, and they allocate objects from the same heap. Although this makes it easier for threads to share information, you must be careful that they do not interfere with other threads in the same process.

Thread operations (thread state)

Spawn: A thread is spawned within a process, either by a process or by a thread. Block: If a thread is waiting for an event to occur during execution, it is blocked. Unblock: If an event that blocks a thread occurs, the thread is activated and put into the ready queue. Schedule: Select a ready thread to enter the execution state. Finish: If a thread finishes, its register context and stack contents are released.

Thread priority

Although we say threads run concurrently. However, this is often not the case. As discussed earlier, executing multiple threads on a single CPU in a certain order is called scheduling when there is only one CPU in the system. Java uses a simple, fixed scheduling method called fixed priority scheduling. This algorithm performs scheduling according to the relative priority of runnable threads. When a thread is created, it inherits the priority of the original thread. The priority can be changed as needed. If there are multiple threads waiting to run at any time, the system selects the runnable thread with the highest priority to run. It can only run if it stops, automatically abandons, or for some reason becomes a non-runnable low-priority thread. If two threads have the same priority, they are run alternately. The Java real-time system’s thread scheduling algorithm is mandatory. At any moment, if a thread with a higher priority than other threads becomes runnable, the real-time system will select that thread to run. An application can set the priority size of a thread by using the thread method setPriority(int).

There’s a thread in a ready state, and you need a thread scheduler to decide when to execute, scheduling according to priority.

Thread state change

  1. Create thread: When a new process is created, a new thread is also created. Threads in the process can create new threads in the same process.
  2. Terminating a thread: It can terminate itself normally, or one thread may execute an error and be forcibly terminated by another thread. The terminating thread operation is responsible for freeing registers and stacks held by the thread.
  3. Blocking thread: When a thread waits for each event to fail, it stops running.
  4. Wake up thread: When a thread blocking event occurs, the state of the blocked thread is put into a ready state and suspended to the ready queue. The process still has an execution-related state. For example, when a process is in the “executing” state, it actually means that a thread in the process is executing. Operations that are imposed on a process in relation to its state also apply to other threads. For example, when a process is suspended, all threads in that process are also suspended, as is activation.

The thread pool

Thread pooling is a form of multithreaded processing in which tasks are added to a queue and then automatically started after a thread is created. Thread pool threads are background threads. Each thread uses the default stack size, runs at the default priority, and is in a multithreaded cell. Thread pool: A thread usage pattern. Too many lines will bring scheduling overhead, which will affect cache locality and overall performance. A thread pool maintains multiple threads, waiting for the supervisor to assign tasks that can be executed concurrently. This avoids the cost of creating and destroying threads while working on short-duration tasks. Thread pools not only ensure full utilization of the kernel, but also prevent overscheduling. The number of threads available should depend on the number of concurrent processors available, processor cores, memory, network sockets, and so on. For example, the number of threads is usually the number of cpus +2, because too many threads cause additional thread switching overhead.

A common way to schedule tasks to execute threads is to use synchronous queues, called task queues. Threads in the pool wait for tasks in the queue and put the completed tasks into the completion queue.

multithreading

Multithreading is a technique that enables concurrent execution of multiple threads from software or hardware. Multithreaded computers have hardware that allows them to execute more than one thread at a time, improving overall processing performance. Systems with this capability include symmetric multiprocessors, multicore processors, and chip-level multiprocessors or simultaneous multithreading processors. In a program, these independently running pieces of program are called threads, and the concept of programming with them is called multithreading.

Introduction to the

In computer programming, a basic concept is to control multiple tasks simultaneously. Many programming problems require the program to stop what it is doing, work on some other problem, and then return to the main process. This can be done in a number of ways. In the beginning, programmers who mastered the machine’s low-level languages wrote interrupt service routines, in which the main process was suspended by a hardware-level interrupt. While this is a useful approach, the resulting programs are difficult to migrate, creating another class of costly problems. Interrupts are necessary for tasks that are very real-time. For many other problems, however, it is only necessary to divide the problem into separate running pieces of program so that the whole program can more quickly respond to user requests.

At first, threads were just a tool for allocating the processing time of individual processors. But if the operating system itself supports multiple processors, then each thread can be assigned to a different processor, truly entering the “parallel operation” state. From a programming language perspective, one of the most valuable features of multithreading is that programmers don’t have to care how many processors are used. The program is logically divided into several threads; If the machine itself has multiple processors installed, the program will run faster without any special tuning.

Based on the previous discussion, you might feel that threading is very simple. But there is one thing you must pay attention to: sharing resources! If you have multiple threads running at the same time and they are trying to access the same resource, you run into a problem. For example, two threads cannot send information to a printer at the same time. To solve this problem, shareable resources (such as printers) must be locked for the duration of use. So one thread can lock a resource and, after completing its task, unlock (release) the lock so that other threads can then use the same resource.

Multithreading is to complete multiple tasks synchronously, not to improve operation efficiency, but to improve the efficiency of resource use to improve the efficiency of the system. Threads are implemented when multiple tasks need to be completed at the same time.

The principle of

Multithreading is a concurrent execution mechanism. Concurrent execution mechanism principle: simply is to make a processor is divided into several short time slices, each time slice took turns to perform processing various applications, due to a short time, relative to an application, as the processor for their own services, so as to achieve the effect of multiple applications at the same time. Multithreading is the operating system of this concurrent execution mechanism principle in a program, a program is divided into a number of sub-tasks, multiple sub-tasks concurrent execution, each task is a thread. This is a multithreaded program.

advantages

  1. Using threads allows you to put tasks in a long-running program in the background.
  2. The user interface can be more attractive, so that if a user clicks a button to trigger the processing of some event, a progress bar pops up to show the progress of the processing.
  3. Programs may run faster.
  4. Threads are useful for waiting tasks such as user input, file reading and writing, and network data sending and receiving. In this case, valuable resources such as memory footprint can be freed up.
  5. Multithreading technology also plays an important role in iOS software development.

disadvantages

  1. If you have a large number of threads, performance is affected because the operating system needs to switch between them.
  2. More threads require more memory space.
  3. Threads can cause more “bugs” in your program, so use them with caution.
  4. Thread termination requires consideration of its impact on program execution.
  5. Block model data is typically shared between multiple threads, and thread deadlocks need to be prevented.

advantage

Multi-process program structure is very different from multi-thread program structure. Multi-thread program structure has the following advantages over multi-process program structure:

  1. Convenient communication and data exchange

There is a convenient communication and data exchange mechanism between threads. For different processes, they have independent data space, and data transfer can only be carried out through communication, which is not only time-consuming, but also very inconvenient. Threads, on the other hand, share data space between threads in the same process, so data from one thread can be directly used by other threads, which is not only fast, but also convenient. 2. Use the CPU more efficiently Using multiple threads speeds up application responsiveness. This is especially meaningful for the graphical interface program, when an operation takes a long time, the whole system will wait for the operation, at this time the program will not respond to the keyboard, mouse, menu operation, and the use of multi-threading technology, put the time-consuming operation in a new thread, can avoid this embarrassing situation. At the same time, multithreading makes multi-CPU systems more efficient. The operating system ensures that different threads run on different cpus when the number of threads does not exceed the number of cpus.

Concept to distinguish

Threads and multithreading: Threads are the system’s code execution process, if the system as an employee, assigned to perform a task, he will not respond to any other task. Only when the task is completed can you reassign it to him. Every program has a main thread that performs the necessary tasks for the program. When we work on a costly task (such as uploading or downloading an image), if we let the main thread perform the task, it will wait until the action is complete before continuing with the code. During this time, the main thread is “busy,” that is, unable to perform any other functions. Reflected in the interface is that the user interface is completely “stuck”. Multi-threading is the process of separating linear tasks into several sub-tasks for simultaneous execution. The advantage of this method is to prevent thread “clogging” and enhance user experience and program efficiency. The downside is that the code becomes much more complex, and the hardware requirements are correspondingly higher.

application

Most of the world’s computers, then and now, still use the von Neumann architecture, which is characterized by sequential processing, in which a processor can only do one thing at a time. Windows 95/NT uses a new task scheduling strategy, it divides a process into multiple threads, each thread takes turns to occupy CPU computing time, the operating system constantly suspends the thread, wakes up, suspends again, calls again, and so on, because the CPU speed is relatively fast now, It feels like multiple threads are executing simultaneously, as if there are multiple cpus in a computer.

Concurrency and parallelism

Concurrency: The execution of two or more programs in the same time period, with temporal overlap (simultaneous macroscopically, sequential microscopically). In an operating system, concurrency refers to a period of time when several programs are running between start and finish, and all of these programs are running on the same processor, but only one program is running on the processor at any time. Parallelism: In operating systems, the execution of a group of programs at independent, asynchronous speeds, not equal to overlap in time (occurring at the same time).

The difference between concurrency and parallelism

Concurrent when there are multiple threads in operation, if the system is only one CPU, it is impossible to truly for more than one thread at the same time, it can only divide the CPU run time into several time periods, then time period assigned to each thread execution, threads run code in a time period, other threads in a hang. This approach is called Concurrent.

Parallel: When the system has more than one CPU, the operation of the thread may be non-concurrent. When one CPU executes a thread, another CPU can execute another thread. The two threads do not occupy CPU resources, but can execute simultaneously. This method is called Parallel.

Difference: Concurrency and parallelism are both similar and different concepts. Parallelism refers to two or more events occurring at the same time. Concurrency is when two or more events occur at the same time interval. In the multi-program environment, concurrency refers to the fact that there are multiple programs running concurrently at a macro level in a period of time, but in the single-processor system, only one program can be executed at any time, so these programs can only be executed alternately at a time. If there are multiple processors in the computer system, the programs that can be executed concurrently can be distributed to multiple processors to achieve parallel execution, that is, each processor is used to process a program that can be executed concurrently, so that multiple programs can be executed simultaneously.

The queue mechanism

Queue is a kind of data structure, it has the characteristic of first in first out, it is a kind of widely used structure. In order to improve the efficiency of working between computers or between computers, we often use queue mechanism. Queue mechanism is simply based on queues, using a certain scheme to improve work efficiency. For example, in an operating system, jobs, processes, and threads are scheduled based on queues.

Relationship between queues and threads

Task: Each execution of a piece of code, such as downloading an image, triggers a network request. Queues: Queues are used to organize tasks, and a queue contains multiple tasks. A queue is a description of a task. It can contain multiple tasks. It is a description of the application layer. Threads are the scheduling unit at the system level, which is a more low-level description. Multiple tasks on a queue (parallel queue) may be assigned to multiple threads for execution. In iOS, the main thread is a thread, and the main queue refers to the organization of tasks on the main thread. The main queue is executed only on the main thread, but not necessarily on the main thread. It may be another synchronized queue. Synchronous operations do not open up new threads, so when you customize a synchronous serial or parallel queue, it is still executed on the main thread.

Synchronous and asynchronous

Thread synchronization: When one thread is working on a memory address, no other thread can work on the memory address until the thread completes the operation, and the other threads are waiting. Another meaning of asynchrony is the asynchronous processing of computer multithreading. In contrast to synchronous processing, asynchronous processing does not block the current thread to wait for processing to complete, but allows subsequent operations until another thread has completed processing, with a callback notifying that thread.

Sync: Only tasks can be executed on the current thread in sequence and new threads cannot be started. (Blocks the current thread and waits for the task to complete.) Asynchronous Async: Performs tasks in a new thread and has the ability to start a new thread. (Do not block the current thread, do not wait for the task to complete)

It’s important to remember that when we talk about synchronous and asynchronous in daily development, we refer to threads. Serial, parallel, and concurrent are queues. Don’t confuse them.

Refer to the link

Reference link :🔗

  • IOS Low-level learning – The fundamentals of Multithreading

  • IOS Interview prep – Multithreading

  • Process – Baidu Encyclopedia entry

  • Thread – Baidu Encyclopedia entry

  • Process control block – Baidu Encyclopedia entry