• thread

  • process

Process: is a basic unit of concurrently executing programs that allocates and manages resources during execution, and is a dynamic concept that competes for the resources of a computer system.

Thread: An execution unit of a process, a scheduling entity within the process section. A basic unit of independent operation that is smaller than a process. Threads are also known as lightweight processes.

A program has at least one processor and a process has at least one thread.

Differences between processes and threads

Address space: Threads of the same process share the address space of this process, while processes are independent of each other.

Resource ownership: Threads within the same process share the resources of this process such as memory, I/O, CPU, etc., but the resources between processes are independent.

When a process crashes, it does not affect other processes in protected mode, but a thread crashes and the whole process dies. So multi-process is more robust than multi-thread.

Process switching consumes more resources and is more efficient. So when it comes to frequent switching, it is better to use threads than processes. Similarly, if concurrent operations are required and some variables are shared, only threads can be used and not processes.

Execution process: Each independent process has a program entry, a sequential execution sequence, and a program entry. However, threads cannot be executed independently and must be dependent on the application, which provides control over the execution of multiple threads.

Threads are the basic unit of processor scheduling, but processes are not.

Both can execute concurrently.

Thread safety is a concept in computer program code when programming with multiple threads.

In programs where multiple threads with shared data are executing in parallel, thread-safe code ensures that each thread can execute properly and correctly without surprises such as data contamination through synchronization mechanisms.

Thread safety problems are mostly caused by global and static variables, and local variable escapes may also cause thread safety problems. If each thread has only read operations on global variables and static variables, but no write operations, the global variables are generally thread-safe; if multiple threads perform write operations at the same time, thread synchronization should be considered, otherwise, thread safety may be affected.