1: Thread theory
1. What is a thread?
Each process has its own address space, that is, process space. A server usually needs to receive a large number of concurrent requests, and create a process for each request. The system overhead is high and the request response efficiency is low, so the operating system introduces threads.
So the process is actually a resource unit that is really used CPU The execution is actually the thread in the process

- PCB: pcb is a printed circuit board, also known as printed circuit board. They are: insulating base plate, connecting wire and bonding pad for assembling and welding electronic components. "
- TCB: transmission control block
2. Abstract metaphor thread
- 1. The thread travels under the process (a simple carriage cannot run)
- 2. A process can contain multiple threads (a train can have multiple carriages)
- 3. It is difficult to share data between different processes (it is difficult for passengers on one train to change to another train, such as station transfer)
- )It is easy to share data between different threads in the same process (it is easy to change carriage A to carriage B)
- 4. Processes consume more computer resources than threads (using multiple trains consumes more resources than multiple carriages)
- 5. Processes will not interact with each other. If one thread hangs, the whole process will hang (one train will not affect another train, but if one carriage in the middle of a train catches fire, it will affect all carriages)
- 6. The process can be extended to multiple machines, and the process is suitable for multiple cores at most (different trains can run on multiple tracks, and the carriage of the same train cannot run on different tracks)
- 7. The memory address used by the process can be locked, that is, when a thread uses some shared memory, other threads must wait for it to end before they can use this memory. (like the bathroom on the train) - "mutex"
- 8. The memory address used by the process can be limited (for example, how many people are allowed to enter the restaurant on the train at most. If it is full, you need to wait at the door and wait until someone comes out) - "semaphore"

The process is similar to the factory thread, which is similar to the pipeline in the factory
All processes must contain at least one thread
3. Thread characteristics
In a multithreaded operating system, it usually includes multiple threads in a process, and each thread is used as a resource CPU The basic unit of is the entity that costs the least. The thread has the following properties.
- 1) Light entity
The entities in the thread basically do not have system resources, but have some essential resources that can ensure independent operation.
Thread entities include programs, data, and TCB. Thread is a dynamic concept, and its dynamic characteristics are described by Thread Control Block (TCB).
TCB Include the following information
- 2) The basic unit of independent scheduling and dispatch.
In multithreaded OS, thread is the basic unit that can run independently, so it is also the basic unit of independent scheduling and dispatching. Because threads are "light", thread switching is very fast and low overhead (in the same process).
- 3) Share process resources.
Each thread in the same process can share the resources owned by the process. Firstly, all threads have the same process id, which means that threads can access each memory resource of the process; In addition, you can also access the open files, timers, semaphore mechanisms, etc. owned by the process. Because threads in the same process share memory and files, threads communicate with each other without calling the kernel.
- 4) Can be executed concurrently.
- Multiple threads in a process can execute concurrently, and even all threads in a process can execute concurrently; Similarly, threads in different processes can also execute concurrently, making full use of and giving full play to the ability of the processor to work in parallel with peripheral devices.
4. Threads in memory

Multiple threads share resources in the address space of the same process. It is a simulation of multiple processes on a computer. Sometimes, threads are called lightweight processes.
1.For multiple processes on a computer, other physical resources such as physical memory, disk, printer and so on are shared. Multithreading is similar to multithreading cpu Fast switching between multiple threads.
2.Different processes are hostile, seizing and competing with each other cpu The relationship between Xunlei and QQ Grab resources. The same process is created by a programmer's program, so threads in the same process are cooperative. One thread can access the memory address of another thread, which is shared by everyone. If one thread kills the memory of another thread, it is purely a programmer's brain problem.
3.Similar to a process, each thread also has its own stack. Unlike a process, the thread library cannot use clock interrupts to force threads to give up CPU,Can call thread_yield The running thread is automatically abandoned cpu,Let another thread run.
4.Thread is usually beneficial, but it brings great difficulty in small program design. The problem of thread is:
1. If the parent process has multiple threads, does the child thread need the same number of threads
2. In the same process, what if one thread closes the file and another thread is preparing to write to the file?
Therefore, in multithreaded code, more attention is needed to design program logic and protect program data.
2: Two ways to set up threads
1. What should be done to set up the process
1.Reapply a piece of memory space
2.Import all the required resources
2. What operations are required to set up threads
Neither of the above two steps is required, so setting up a thread consumes far less resources than setting up a process!!!
3: Create thread (easy version)
from threading import Thread
1. Create thread
from threading import Thread
import time
def test(name):
print('%s is running' % name)
time.sleep(3)
print('%s is over' % name)
t = Thread(target=test, args=('jason',))
t.start()
print('main')
jason is running
main
jason is over
2. Class creation thread
from threading import Thread
import time
class MyClass(Thread):
def __init__(self,name):
super().__init__()
self.name = name
# The parent class has its own run. The child process overrides the run method itself
def run(self):
print('%s is running' % self.name)
time.sleep(3)
print('%s is over' % self.name)
obj = MyClass('jason')
# Call the parent class to create a child process
obj.start()
print('Main thread')
jason is running
Main thread
jason is over
4: Other methods (modules) of Thread objects
- 1.join method
- 2. Obtain the process number (verify that multiple threads can be set up in the same process, but the data of multiple threads in the same process are shared)
- 3.active_count Counts the number of threads that are currently active
- 4.current_thread View main thread name
5: The join method creates a thread
from threading import Thread,active_count,current_thread
import time
import os
def test(name):
# View sub thread process number
print(os.getpid())
# View child thread name
print(current_thread().name)
print('%s is running' % name)
time.sleep(3)
print('%s is over' % name)
# Create child thread
t = Thread(target=test, args=('jason',))
# Execute child thread
t.start()
print(os.getpid())
# Execute the child thread before the main thread
t.join()
# View main thread name
print(current_thread().name)
# View the main thread process number
print(active_count())
print('main')

140264
Thread-1
jason is running
140264
jason is over
MainThread
1
main
5: Daemon thread
1. Concept of daemon thread
1.The end of the main thread means the end of the daemon
2.Therefore, the main thread needs to wait for the end of all non daemon threads
3.The daemon will end with the end of the main process
2. Code implementation (daemon)
from threading import Thread
from multiprocessing import Process
import time
def foo():
print(123)
time.sleep(3)
print("end123")
def bar():
print(456)
time.sleep(1)
print("end456")
if __name__ == '__main__':
t1=Thread(target=foo)
t2=Thread(target=bar)
t1.daemon=True
t1.start()
t2.start()
print("main-------")
123
456
main-------
end456
6: Thread data sharing
- Inter process data is isolated by default, but multiple threads in the same process share data
from threading import Thread
money = 100
def test():
global money
money = 999
t = Thread(target=test)
t.start()
t.join()
print(money)
7: Thread mutex
1. What effect can mutex achieve?
Lock can achieve the effect of changing concurrency into serial
2. Why use mutex?
1.Through the execution result, the address mutex can ensure that multiple threads access shared data without data error
2.The same data cannot be operated concurrently,Operating the same data under concurrent conditions is extremely easy to cause data disorder
3.Mutex locks the shared data to ensure that only one thread can operate at the same time.

3. Use the module
Lock Variable, which is essentially a function. You can obtain a mutex by calling this function
4. Thread mutex (build)
from threading import Thread, Lock
from multiprocessing import Lock
import time
num = 101
# Modify value
def test(mutex):
# Local modification Global
global num
# Lock (only one person can pass at a time)
mutex.acquire()
# Get the value of num first
tmp = num
# Simulated delay effect
time.sleep(0.1)
# Defined variable data - number of circular accesses
tmp -= 1
num = tmp
# Release lock
mutex.release()
# Define an empty dictionary
t_list = []
# Create mutex
mutex = Lock()
# Cycle 1-100
for i in range(100):
# Create child thread
t = Thread(target=test, args=(mutex,))
# Execute child thread
t.start()
# Add execution child thread to empty list
t_list.append(t)
# Ensure that all child threads end
for t in t_list:
# The serial executes the child thread first and then the main thread
t.join()
print(num)
8: TCP server implements concurrency