Concurrent programming thread

Posted by vinny199 on Fri, 14 Jan 2022 11:02:11 +0100

1, What is a thread?

Thread (English: thread) is the smallest unit that the operating system can schedule operations. It is included in the process and is the actual operation unit in the process. A thread refers to a single sequential control flow in a process. Multiple threads can be concurrent in a process, and each thread executes different tasks in parallel.

Thread development:
In the 1960s, the basic unit that can have resources and run independently in OS was process. However, with the development of computer technology, process has many disadvantages. First, because process is the owner of resources, there is a large space-time cost in creation, revocation and switching, so it is necessary to introduce light process; Second, due to the emergence of symmetric multiprocessor (SMP), it can meet multiple running units, and the parallel overhead of multiple processes is too large.

Therefore, in the 1980s, Threads, the basic unit that can run independently, appeared.

2, Relationship between process and thread

  1. Address space and other resources (such as open files): processes are independent and shared among threads of the same process. Threads within one process are not visible to other processes.

  2. Communication: interprocess communication IPC. Threads can directly read and write process data segments (such as global variables) for communication - process synchronization and mutual exclusion are required to ensure data consistency.

  3. Scheduling and switching: thread context switching is much faster than process context switching.

  4. In a multithreaded operating system, a process is not an executable entity.

3, Thread characteristics

  1. Multiple threads share the address space of a process

  2. Threads are lighter than processes, and threads are easier to create revocable than processes. In many operating systems, creating a thread is 10-100 times faster than creating a process. This feature is very useful when a large number of threads need dynamic and rapid modification

  3. If multiple threads are cpu intensive, there is no performance enhancement, but if there is a large amount of computing and a large amount of I/O processing, having multiple threads allows these activities to overlap each other, which will speed up the execution of the program.

  4. In a multi cpu system, in order to maximize the use of multi-core, you can start multiple threads, which is much less expensive than starting processes. (this does not apply to python)

4, python module threading

In python, the module threading is used to operate threads. The usage of threading module is basically the same as that of multiprocess.

There are two ways to create threads

Mode 1:

from threading import Thread
import time

def test(name):
    print('%s say hello' %name)

if __name__ == '__main__':
    print('Main thread')

Mode 2:

from threading import Thread
import time

class MyThread(Thread):
    def __init__(self,name):

    def run(self):
        print('%s say hello' %

if __name__ == '__main__':
    t = MyThread('xie')
    print('Main thread')

Data sharing between threads in the same process

from  threading import Thread

def func():
	global n

if __name__ == 'main':
	print('main',n)  # Master 0

Other methods in the thread module

# t.isAlive(): Returns whether the thread is active
# t.getName(): returns the thread name
# t.setName(): sets the thread name

# threading.currentThread(): returns the current thread variable
# threading.enumerate(): returns a list of running threads. Running refers to the thread after starting and before ending, excluding the threads before starting and after termination
# threading.activeCount(): returns the number of running threads, which is the same as len(threading.enumerate())

Daemon thread

Stops and is recycled as the main thread finishes running. For the main thread, running completion means that all non daemon threads in the process where the main thread is located have finished running, and the main thread is considered to have finished running.

from threading import Thread
import time

def test(name):
    print('%s say hello' %name)

if __name__ == '__main__':
    t.daemon = True  # Must be set before t.start()

    print('Main thread')
    Main thread
from threading import Thread
import time

def foo():

def bar():


# Output sequence:
# 123
# 456
# main-------
# end123
# end456